keyring 25.6.0


pip install keyring

  Latest version

Released: Dec 25, 2024

Project Links

Meta
Author: Kang Zhang
Maintainer: Jason R. Coombs
Requires Python: >=3.9

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers

License
  • OSI Approved :: MIT License

Programming Language
  • Python :: 3
  • Python :: 3 :: Only
https://img.shields.io/pypi/v/keyring.svg https://img.shields.io/pypi/pyversions/keyring.svg tests Ruff https://readthedocs.org/projects/keyring/badge/?version=latest https://img.shields.io/badge/skeleton-2024-informational https://tidelift.com/badges/package/pypi/keyring Join the chat at https://gitter.im/jaraco/keyring

The Python keyring library provides an easy way to access the system keyring service from python. It can be used in any application that needs safe password storage.

These recommended keyring backends are supported:

Other keyring implementations are available through Third-Party Backends.

Installation - Linux

On Linux, the KWallet backend relies on dbus-python, which does not always install correctly when using pip (compilation is needed). For best results, install dbus-python as a system package.

Compatibility - macOS

macOS keychain supports macOS 11 (Big Sur) and later requires Python 3.8.7 or later with the “universal2” binary. See #525 for details.

Using Keyring

The basic usage of keyring is pretty simple: just call keyring.set_password and keyring.get_password:

>>> import keyring
>>> keyring.set_password("system", "username", "password")
>>> keyring.get_password("system", "username")
'password'

Command-line Utility

Keyring supplies a keyring command which is installed with the package. After installing keyring in most environments, the command should be available for setting, getting, and deleting passwords. For more usage information, invoke with no arguments or with --help as so:

$ keyring --help
$ keyring set system username
Password for 'username' in 'system':
$ keyring get system username
password

The command-line functionality is also exposed as an executable package, suitable for invoking from Python like so:

$ python -m keyring --help
$ python -m keyring set system username
Password for 'username' in 'system':
$ python -m keyring get system username
password

Tab Completion

If installed via a package manager (apt, pacman, nix, homebrew, etc), these shell completions may already have been distributed with the package (no action required).

Keyring provides tab completion if the completion extra is installed:

$ pip install 'keyring[completion]'

Then, generate shell completions, something like:

$ keyring --print-completion bash | sudo tee /usr/share/bash-completion/completions/keyring
$ keyring --print-completion zsh | sudo tee /usr/share/zsh/site-functions/_keyring
$ keyring --print-completion tcsh | sudo tee /etc/profile.d/keyring.csh

Note: the path of /usr/share is mainly for GNU/Linux. For other OSs, consider:

  • macOS (Homebrew x86): /usr/local/share

  • macOS (Homebrew ARM): /opt/homebrew/share

  • Android (Termux): /data/data/com.termux/files/usr/share

  • Windows (mingw64 of msys2): /mingw64/share

After installing the shell completions, enable them following your shell’s recommended instructions. e.g.:

  • bash: install bash-completion, and ensure . /usr/share/bash-completion/bash_completion in ~/.bashrc.

  • zsh: ensure autoload -Uz compinit && compinit appears in ~/.zshrc, then grep -w keyring ~/.zcompdump to verify keyring appears, indicating it was installed correctly.

Configuring

The python keyring lib contains implementations for several backends. The library will attempt to automatically choose the most suitable backend for the current environment. Users may also specify the preferred keyring in a config file or by calling the set_keyring() function.

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 keyring diagnose.

Config file content

To specify a keyring backend, set the default-keyring option to the full path of the class for that backend, such as keyring.backends.macOS.Keyring.

If keyring-path is indicated, keyring will add that path to the Python module search path before loading the backend.

For example, this config might be used to load the SimpleKeyring from the simplekeyring module in the ./demo directory (not implemented):

[backend]
default-keyring=simplekeyring.SimpleKeyring
keyring-path=demo

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:

Write your own keyring backend

The interface for the backend is defined by keyring.backend.KeyringBackend. Every backend should derive from that base class and define a priority attribute and three functions: get_password(), set_password(), and delete_password(). The get_credential() function may be defined if desired.

See the backend module for more detail on the interface of this class.

Keyring employs entry points to allow any third-party package to implement backends without any modification to the keyring itself. Those interested in creating new backends are encouraged to create new, third-party packages in the keyrings namespace, in a manner modeled by the keyrings.alt package. See the setup.cfg file in that project for hints on how to create the requisite entry points. Backends that prove essential may be considered for inclusion in the core library, although the ease of installing these third-party packages should mean that extensions may be readily available.

To create an extension for Keyring, please submit a pull request to have your extension mentioned as an available extension.

Runtime Configuration

Keyring additionally allows programmatic configuration of the backend calling the api set_keyring(). The indicated backend will subsequently be used to store and retrieve passwords.

To invoke set_keyring:

# define a new keyring class which extends the KeyringBackend
import keyring.backend

class TestKeyring(keyring.backend.KeyringBackend):
    """A test keyring which always outputs the same password
    """
    priority = 1

    def set_password(self, servicename, username, password):
        pass

    def get_password(self, servicename, username):
        return "password from TestKeyring"

    def delete_password(self, servicename, username):
        pass

# set the keyring for keyring lib
keyring.set_keyring(TestKeyring())

# invoke the keyring lib
try:
    keyring.set_password("demo-service", "tarek", "passexample")
    print("password stored successfully")
except keyring.errors.PasswordSetError:
    print("failed to store password")
print("password", keyring.get_password("demo-service", "tarek"))

Disabling Keyring

In many cases, uninstalling keyring will never be necessary. Especially on Windows and macOS, the behavior of keyring is usually degenerate, meaning it will return empty values to the caller, allowing the caller to fall back to some other behavior.

In some cases, the default behavior of keyring is undesirable and it would be preferable to disable the keyring behavior altogether. There are several mechanisms to disable keyring:

  • Uninstall keyring. Most applications are tolerant to keyring not being installed. Uninstalling keyring should cause those applications to fall back to the behavior without keyring. This approach affects the Python environment where keyring would otherwise have been installed.

  • Configure the Null keyring in the environment. Set PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring in the environment, and the Null (degenerate) backend will be used. This approach affects all uses of Keyring where that variable is set.

  • Permanently configure the Null keyring for the user by running keyring --disable or python -m keyring --disable. This approach affects all uses of keyring for that user.

Altering Keyring Behavior

Keyring provides a mechanism to alter the keyring’s behavior through environment variables. Each backend implements a KeyringBackend.set_properties_from_env, which when invoked will find all environment variables beginning with KEYRING_PROPERTY_{NAME} and will set a property for each {NAME.lower()} on the keyring. This method is invoked during initialization for the default/configured keyring.

This mechanism may be used to set some useful values on various keyrings, including:

  • keychain; macOS, path to an alternate keychain file

  • appid; Linux/SecretService, alternate ID for the application

Using Keyring on Ubuntu 16.04

The following is a complete transcript for installing keyring in a virtual environment on Ubuntu 16.04. No config file was used:

$ sudo apt install python3-venv libdbus-glib-1-dev
$ cd /tmp
$ pyvenv py3
$ source py3/bin/activate
$ pip install -U pip
$ pip install secretstorage dbus-python
$ pip install 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'

Using Keyring on headless Linux systems

It is possible to use the SecretService backend on Linux systems without X11 server available (only D-Bus is required). In this case:

  • Install the GNOME Keyring daemon.

  • Start a D-Bus session, e.g. run dbus-run-session -- sh and run the following commands inside that shell.

  • Run gnome-keyring-daemon with --unlock option. The description of that option says:

    Read a password from stdin, and use it to unlock the login keyring or create it if the login keyring does not exist.

    When that command is started, enter a password into stdin and press Ctrl+D (end of data). After that, the daemon will fork into the background (use --foreground option to block).

  • Now you can use the SecretService backend of Keyring. Remember to run your application in the same D-Bus session as the daemon.

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'

Integration

API

The keyring lib has a few functions:

  • get_keyring(): Return the currently-loaded keyring implementation.

  • get_password(service, username): Returns the password stored in the active keyring. If the password does not exist, it will return None.

  • get_credential(service, username): Return a credential object stored in the active keyring. This object contains at least username and password attributes for the specified service, where the returned username may be different from the argument.

  • set_password(service, username, password): Store the password in the keyring.

  • delete_password(service, username): Delete the password stored in keyring. If the password does not exist, it will raise an exception.

In all cases, the parameters (service, username, password) should be Unicode text.

Exceptions

The keyring lib raises the following exceptions:

  • keyring.errors.KeyringError: Base Error class for all exceptions in keyring lib.

  • keyring.errors.InitError: Raised when the keyring cannot be initialized.

  • keyring.errors.PasswordSetError: Raised when the password cannot be set in the keyring.

  • keyring.errors.PasswordDeleteError: Raised when the password cannot be deleted in the keyring.

Get Involved

Python keyring lib is an open community project and eagerly welcomes contributors.

Security Considerations

Each built-in backend may have security considerations to understand before using this library. Authors of tools or libraries utilizing keyring are encouraged to consider these concerns.

As with any list of known security concerns, this list is not exhaustive. Additional issues can be added as needed.

  • macOS Keychain
    • Any Python script or application can access secrets created by keyring from that same Python executable without the operating system prompting the user for a password. To cause any specific secret to prompt for a password every time it is accessed, locate the credential using the Keychain Access application, and in the Access Control settings, remove Python from the list of allowed applications.

  • Freedesktop Secret Service
    • No analysis has been performed

  • KDE4 & KDE5 KWallet
    • No analysis has been performed

  • Windows Credential Locker
    • No analysis has been performed

Making Releases

This project makes use of automated releases and continuous integration. The simple workflow is to tag a commit and push it to Github. If it passes tests in CI, it will be automatically deployed to PyPI.

Other things to consider when making a release:

  • Check that the changelog is current for the intended release.

Running Tests

Tests are continuously run in Github Actions.

To run the tests locally, install and invoke tox.

Background

The project was based on Tarek Ziade’s idea in this post. Kang Zhang initially carried it out as a Google Summer of Code project, and Tarek mentored Kang on this project.

For Enterprise

Available as part of the Tidelift Subscription.

This project and the maintainers of thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use.

Learn more.

25.6.0 Dec 25, 2024
25.5.0 Oct 26, 2024
25.4.1 Sep 20, 2024
25.4.0 Sep 17, 2024
25.3.0 Aug 02, 2024
25.2.1 May 13, 2024
25.2.0 Apr 26, 2024
25.1.0 Apr 02, 2024
25.0.1 Apr 02, 2024
25.0.0 Mar 23, 2024
24.3.1 Feb 27, 2024
24.3.0 Nov 12, 2023
24.2.0 Jun 24, 2023
24.1.1 Jun 24, 2023
24.1.0 Jun 23, 2023
24.0.1 Jun 22, 2023
24.0.0 Jun 20, 2023
23.13.1 Dec 18, 2022
23.13.0 Dec 18, 2022
23.12.1 Dec 18, 2022
23.11.0 Nov 05, 2022
23.10.0 Nov 04, 2022
23.9.3 Sep 17, 2022
23.9.2 Sep 17, 2022
23.9.1 Sep 04, 2022
23.9.0 Sep 01, 2022
23.8.2 Aug 08, 2022
23.8.1 Aug 07, 2022
23.8.0 Aug 07, 2022
23.7.0 Jul 14, 2022
23.6.0 Jun 08, 2022
23.5.1 May 23, 2022
23.5.0 Jan 02, 2022
23.4.1 Jan 02, 2022
23.4.0 Nov 28, 2021
23.3.0 Nov 25, 2021
23.2.1 Sep 12, 2021
23.2.0 Sep 11, 2021
23.1.0 Aug 16, 2021
23.0.1 Mar 25, 2021
23.0.0 Mar 06, 2021
22.4.0 Mar 06, 2021
22.3.0 Feb 27, 2021
22.2.0 Feb 26, 2021
22.1.0 Feb 26, 2021
22.0.1 Jan 26, 2021
22.0.0 Jan 24, 2021
21.8.0 Dec 30, 2020
21.7.0 Dec 22, 2020
21.6.0 Dec 22, 2020
21.5.0 Nov 07, 2020
21.4.0 Aug 30, 2020
21.3.1 Aug 22, 2020
21.3.0 Aug 02, 2020
21.2.1 May 01, 2020
21.2.0 Mar 15, 2020
21.1.1 Mar 06, 2020
21.1.0 Jan 11, 2020
21.0.0 Dec 24, 2019
20.0.1 Dec 24, 2019
20.0.0 Dec 08, 2019
19.3.0 Nov 30, 2019
19.2.0 Sep 11, 2019
19.1.0 Aug 22, 2019
19.0.2 May 17, 2019
19.0.1 Mar 24, 2019
19.0.0 Mar 22, 2019
18.0.1 Mar 24, 2019
18.0.0 Feb 13, 2019
17.1.1 Dec 23, 2018
17.1.0 Dec 23, 2018
17.0.0 Nov 30, 2018
16.1.1 Nov 26, 2018
16.1.0 Nov 17, 2018
16.0.2 Nov 08, 2018
16.0.1 Nov 06, 2018
16.0.0 Oct 28, 2018
15.2.0 Oct 27, 2018
15.1.0 Sep 14, 2018
15.0.0 Sep 07, 2018
13.2.1 Jul 06, 2018
13.2.0 Jul 04, 2018
13.1.0 Jun 25, 2018
13.0.0 Jun 17, 2018
12.2.1 May 15, 2018
12.2.0 Apr 26, 2018
12.1.0 Apr 24, 2018
12.0.2 Apr 24, 2018
12.0.1 Apr 06, 2018
12.0.0 Mar 19, 2018
11.1.0 Mar 19, 2018
11.0.0 Jan 29, 2018
10.6.0 Jan 07, 2018
10.5.1 Dec 15, 2017
10.5.0 Nov 13, 2017
10.4.0 Jun 24, 2017
10.3.3 Jun 04, 2017
10.3.2 Apr 09, 2017
10.3.1 Mar 20, 2017
10.3 Feb 28, 2017
10.2 Jan 11, 2017
10.1 Dec 04, 2016
10.0.2 Oct 20, 2016
10.0.1 Oct 20, 2016
10.0 Oct 20, 2016
9.3.1 Jul 14, 2016
9.3 Jun 27, 2016
9.2.1 Jun 26, 2016
9.2 Jun 26, 2016
9.1 Jun 22, 2016
9.0 Apr 10, 2016
8.7 Apr 02, 2016
8.6.1 Apr 02, 2016
8.6 Apr 02, 2016
8.5.1 Mar 13, 2016
8.5 Mar 12, 2016
8.4.1 Feb 15, 2016
8.4 Feb 07, 2016
8.3 Feb 04, 2016
8.2 Jan 27, 2016
8.1.1 Jan 26, 2016
8.1 Jan 25, 2016
8.0.1 Jan 25, 2016
8.0 Jan 22, 2016
7.3.1 Jan 22, 2016
7.3 Jan 14, 2016
7.2 Jan 13, 2016
7.1.2 Jan 11, 2016
7.1.1 Jan 10, 2016
7.1 Jan 10, 2016
7.0.2 Jan 10, 2016
7.0.1 Jan 10, 2016
7.0 Jan 09, 2016
6.1.1 Jan 08, 2016
6.1 Jan 07, 2016
6.0 Jan 05, 2016
5.7.1 Dec 13, 2015
5.7 Dec 07, 2015
5.6 Oct 11, 2015
5.5.1 Oct 11, 2015
5.5 Oct 11, 2015
5.4 Aug 07, 2015
5.3 Feb 25, 2015
5.2.1 Feb 24, 2015
5.2 Feb 24, 2015
5.1 Feb 24, 2015
5.0 Feb 04, 2015
4.1.1 Feb 02, 2015
4.1 Jan 26, 2015
4.0 Jul 31, 2014
3.8 May 11, 2014
3.7 Mar 21, 2014
3.6 Mar 04, 2014
3.5 Feb 14, 2014
3.4 Feb 12, 2014
3.3 Nov 29, 2013
3.2.1 Nov 17, 2013
3.2 Oct 27, 2013
3.1 Oct 22, 2013
3.0.5 Sep 18, 2013
3.0.4 Sep 18, 2013
3.0.3 Sep 09, 2013
3.0.2 Sep 07, 2013
3.0.1 Sep 02, 2013
3.0 Sep 02, 2013
2.1.1 Sep 01, 2013
2.1 Aug 31, 2013
2.0.3 Aug 30, 2013
2.0.2 Aug 27, 2013
2.0.1 Aug 25, 2013
2.0 Aug 18, 2013
1.6.1 Jul 23, 2013
1.6 Jul 13, 2013
1.5 Jun 23, 2013
1.4 Jun 07, 2013
1.3 May 25, 2013
1.2.3 May 25, 2013
1.2.2 Feb 10, 2013
1.2.1 Feb 07, 2013
1.2 Jan 05, 2013
1.2.dev0 Jan 05, 2013
1.1.2 Jan 02, 2013
1.1.1 Jan 02, 2013
1.1 Jan 02, 2013
1.0 Nov 30, 2012
0.10.1 Nov 29, 2012
0.10 Nov 12, 2012
0.9.3 Nov 29, 2012
0.9.2 Jun 18, 2012
0.9.1 Jun 03, 2012
0.9 Apr 20, 2012
0.8.1 Mar 05, 2012
0.8 Feb 22, 2012
0.7.1 Jan 22, 2012
0.7 Dec 29, 2011
0.6.2 Aug 03, 2011
0.5.1 Jan 07, 2011
0.5 Nov 05, 2010
0.4 Sep 12, 2010
0.3 Aug 10, 2010
0.2 Sep 07, 2009
0.1 Aug 21, 2009
0.0rc0 Aug 21, 2009

Wheel compatibility matrix

Platform Python 3
any

Files in release