Reputation: 189
I want to use the Python package keyring to avoid exposing passwords in my Python scripts. According to what I've read, all you need to do is pip install keyring and then start storing and retrieving credentials. I've done this on both my Mac laptop (Big Sur) and a CentOS 7 box. On my Mac, pip list shows that version 23.1.0 of keyring is installed, and the command line utility is available. However, when I try to set a password using the command line or in python using keyring.set_password(...) I get the following error:
File "/lib/python3.7/site-packages/keyring/backends/macOS/init.py", line 38, in set_password api.set_generic_password(self.keychain, service, username, password) NameError: name 'api' is not defined
Line 12 of the init.py is a simple import of the api module:
from . import api
and then line 38, where the error is reported, tries to set the password:
api.set_generic_password(self.keychain, service, username, password)
Why am I getting this error? I read somewhere that you shouldn't install keyring in a venv with pip but I'm not certain why not. Any help is appreciated.
Upvotes: 3
Views: 5318
Reputation: 607
Error was introduced with version keyring==23.1.0
(Aug 15, 2021)
Once I downgraded to keyring==23.0.1
(March 25, 2021), the problem went away.
Upvotes: 1
Reputation: 513
Run this command:
$ /usr/local/lib/python3.7 -c "import keyring.backends._OS_X_API"
That should produce the underlying exception that's causing the module to be missing.
EDIT:
Try adding this
if sys.platform == 'darwin': # It's a solution for Mac
import keyring.backends.OS_X
keyring.set_keyring(keyring.backends.OS_X.Keyring())
else: # It's a solution for Windows
import keyring.backends.Windows
keyring.set_keyring(keyring.backends.Windows.WinVaultKeyring())
Upvotes: 0