Set and Retrieve Passwords From Your Systems Password Manager With The keyring Python Module
Note: if you put something directly in Keychain Access, the values you put in for
account are used in a backwards order from what you'd expect. i.e.
TODO: Look at the below for setting them to see how it goes in and if it's reversed or not
Set passwords form the command line with:
(NOTE: confirmed in march 2022 that
PASSWORD_NAME USER_NAME is the proper order)
python3 -m keyring set PASSWORD_NAME USER_NAME
Running that will ask you to put in the password.
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
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.
Install it with either
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
And retrieve them with:
import keyring keyring.get_password("SERVICE", "PASSWORD")
- 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)