---
author: ''
category: ''
date: 2009/12/21 14:39
description: ''
link: ''
priority: ''
slug: BB858
tags: open source, programming, pyqt, python, qt
title: python-keyring is seriously nice
type: text
updated: 2009/12/21 14:39
url_type: ''
---
Many programs require passwords from the user.
It's nice when a program can remember the password you give it.
It's nicer when it stores said password safely. However, it's not trivial to do that if you care for cross-platform support.
Or at least it wasn't until Kang Zhang wrote `python keyring `_, a module that abstracts the password storage mechanisms for KDE, GNOME, OSX and windows (and adds a couple of file-based backends just in case).
So, how does it work?
Install it in the usual way. If it's not packaged for your distro/operating system, just use easy_install::
easy_install keyring
You could also get it from mercurial::
hg clone http://bitbucket.org/kang/python-keyring-lib/
The API is simplicity itself. This is how you save a secret:
.. code-block:: python
import keyring
keyring.set_password('keyring_demo','username','thisisabadpassword')
You may get this dialog (or some analog on other platforms):
.. raw:: html
And here's the proof that it was saved correctly (this is KDE's password manager):
.. raw:: html
And how do you get the secret back?
.. code-block:: python
import keyring
print keyring.get_password('keyring_demo','username')
This is how it runs::
$ python load.py
thisisabadpassword
As you can see, the API is as easy as it could possible get. It even chose the KWallet backend automatically because I am in KDE!
Python-keyring is a module that fixes a big problem, so a big thank you to Kang Zhang and Tarek Ziadé (who had the idea)