Python Keyring Lib
lastmod 2026-02-20created 2018-09-0415 edits
Store and access your passwords safely. (Python library)
What is Python keyring lib?
The Python keyring lib provides an easy way to access the system keyring service from python. It can be used in any application that needs safe password storage.
The keyring library is licensed under both the MIT license and the PSF license.
These recommended keyring backends are supported by the Python keyring lib:
- macOS Keychain
- Freedesktop Secret Service supports many DE including GNOME (requires secretstorage)
- KDE4 & KDE5 KWallet (requires dbus)
- Windows Credential Locker
How to install
Config file path
The configuration is stored in a file named "keyringrc.cfg" found in a platform-specific location. To determine where the config file is stored, run the following:
Some keyrings also store the keyring data in the file system. To determine where the data files are stored, run:
Example
set command
get command
Diagnose
- config path - uses the XDG_CONFIG_HOME environment variable.
- data root - uses the XDG_DATA_HOME environment variable.
Python Code Example
import keyring as kr
kr.set_password("GeeksforGeeks", "Dwaipayan", "Geeks@123")
print("Before deleting password ", kr.get_password("GeeksforGeeks", "Dwaipayan"))
kr.delete_password("GeeksforGeeks", "Dwaipayan")
print("After deleting Password", kr.get_password("GeeksforGeeks", "Dwaipayan"))
Retrieving stored username and password:
import keyring as kr
kr.set_password("GeeksforGeeks","Dwaipayan","Geeks@123")
cred = kr.get_credential("GeeksforGeeks","")
print("Username : ",cred.username)
print("Password : ",cred.password)
Third-Party Backends
In addition to the backends provided by the core keyring package for the most common and secure use cases, there are additional keyring backend implementations available for other use cases. Simply install them to make them available:
- keyrings.cryptfile - Encrypted text file storage. - Maps to
keyrings.alt.file.EncryptedKeyring. - keyring_jeepney - a pure Python backend using the secret service DBus API for desktop Linux.
- keyrings.alt - "alternate", possibly-insecure backends, originally part of the core package, but available for opt-in.
- gsheet-keyring - a backend that stores secrets in a Google Sheet. For use with ipython-secrets.
- bitwarden-keyring - a backend that stores secrets in the BitWarden password manager.
- sagecipher - an encryption backend which uses the ssh agent protocol's signature operation to derive the cipher key.
- keyrings.osx_keychain_keys - OSX keychain key-management, for private, public, and symmetric keys.
You can list the backends with the following command:
The output looks something like this:
keyrings.cryptfile.file.EncryptedKeyring (priority: 0.6)
keyring.backends.chainer.ChainerBackend (priority: 10)
keyrings.cryptfile.cryptfile.CryptFileKeyring (priority: 2.5)
keyring.backends.SecretService.Keyring (priority: 5)
keyrings.alt.file.PlaintextKeyring (priority: 0.5)
sagecipher.keyring.Keyring (priority: 1)
keyrings.cryptfile.file.PlaintextKeyring (priority: 0.5)
keyrings.alt.file.EncryptedKeyring (priority: 0.6)
keyring.backends.fail.Keyring (priority: 0)
sagecipher
If you want to use sagecipher, first use ssh-agent/ssh-add.
Next, register the keyring
$ opy -m keyring -b sagecipher.keyring.Keyring set myservice myusername
Password for 'myusername' in 'myservice':
Next, check the keyring
Using Keyring on headless Linux systems in a Docker container
It is possible to use keyring with the SecretService backend in Docker containers as well. All you need to do is install the necessary dependencies and add the --privileged flag to avoid any Operation not permitted errors when attempting to unlock the system's keyring.
The following is a complete transcript for installing keyring on a Ubuntu 18:04 container:
docker run -it -d --privileged ubuntu:18.04
$ apt-get update
$ apt install -y gnome-keyring python3-venv python3-dev
$ python3 -m venv venv
$ source venv/bin/activate # source a virtual environment to avoid polluting your system
$ pip3 install --upgrade pip
$ pip3 install keyring
$ dbus-run-session -- sh # this will drop you into a new D-bus shell
$ echo 'somecredstorepass' | gnome-keyring-daemon --unlock # unlock the system's keyring
$ python
>>> import keyring
>>> keyring.get_keyring()
<keyring.backends.SecretService.Keyring object at 0x7f9b9c971ba8>
>>> keyring.set_password("system", "username", "password")
>>> keyring.get_password("system", "username")
'password'
See dbus-run-session and gnome-keyring-daemon for details.