Set and Retrieve Passwords From Your Systems Password Manager With The keyring Python Module

June - 2021

Note: if you put something directly in Keychain Access, the values you put in for name and account are used in a backwards order from what you'd expect. i.e.

keyring.get_password(NAME, ACCOUNT)

TODO: Look at the below for setting them to see how it goes in and if it's reversed or not

TL;DR

Set passwords form the command line with:

TODO: Verify this is the order to put stuff in

python3 -m keyring set PASSWORD_NAME USERNAME

Then, retrieve them in scripts with:

import keyring 

keyring.get_password(PASSWORD_NAME, USERNAME)

NOTE: Confirmed that this is the order the parameters need to be in to get keys that were added from inside Keychain Access itself


Details

You can store passwords in your system's credential locker (e.g. Keychain Access on macOs and ... on Windows and ... on Linux) with the keyring Python module.

Installation

Install it with either pip3 or pip depending on your configuration. E.g.

pip3 install keyring

Setting Passwords From Scripts

You can set your passwords in a script with:

import keyring

keyring.set_password("SERVICE", "USERNAME", "PASSWORD")

Setting Passwords From The Command Line

You can set passwords directly from the command line with:

python3 -m keyring set SERVICE USERNAME

You'll be prompted to enter your password from there

Retrieving Passwords

And retrieve them with:

import keyring 

keyring.get_password("SERVICE", "PASSWORD")

Notes

  • As far as I can tell "SERVICE" is just for grouping. I simply use my username.
  • I used to add dates to my passwords, but I stopped doing that. (This is more a reminder to myself that to anyone else)