Search code examples
pythonwindowskeyring

keyring.get_password() throws NameError when reading credential from Windows Credential Manager


I am try to implement keyring on a windows system to manage database passwords for connection strings in pyodbc and sqlalchemy. I have tried to ensure the relevant packages from the NameError(s) thrown are installed.

Reason for manually setting the backend in keyring on windows

Here is the code I ran:`

from keyring.backends import Windows
import keyring
keyring.set_keyring(Windows.WinVaultKeyring())
keyring.get_password("Service", "Username")

Here are the errors thrown:

    Traceback (most recent call last):
  File ".\Python310\lib\site-packages\keyring\backends\Windows.py", line 109, in _get_password
    res = win32cred.CredRead(
NameError: name 'win32cred' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".\Python310\lib\site-packages\keyring\core.py", line 55, in get_password
    return get_keyring().get_password(service_name, username)
  File ".\Python310\lib\site-packages\keyring\backends\Windows.py", line 99, in get_password
    res = self._get_password(service)
  File ".\Python310lib\site-packages\keyring\backends\Windows.py", line 112, in _get_password
    except pywintypes.error as e:
NameError: name 'pywintypes' is not defined

Installed packages are listed:

Package Version
astroid 2.9.3
backcall 0.2.0
certifi 2021.10.8
cffi 1.15.0
charset-normalizer 2.0.12
cloudpickle 2.1.0
colorama 0.4.4
comtypes 1.1.11
cryptography 37.0.2
dask 2022.6.1
executing 0.8.3
fsspec 2022.5.0
greenlet 1.1.2
idna 3.3
ipython-genutils 0.2.0
isort 5.10.1
keyring 23.6.0
lazy-object-proxy 1.7.1
locket 1.0.0
mccabe 0.6.1
mistune 0.8.4
MouseInfo 0.1.3
numpy 1.22.3
opencv-python 4.5.5.64
packaging 21.3
pandas 1.4.1
partd 1.2.0
pickleshare 0.7.5
Pillow 9.1.1
pip 22.1.2
platformdirs 2.5.1
pure-eval 0.2.2
PyAutoGUI 0.9.53
pycparser 2.21
PyGetWindow 0.0.9
pylint 2.12.2
PyMsgBox 1.0.9
pyodbc 4.0.32
pyparsing 3.0.7
pyperclip 1.8.2
PyQt6 6.3.1
PyQt6-Qt6 6.3.1
PyQt6-sip 13.4.0
PyRect 0.2.0
PyScreeze 0.1.28
pytesseract 0.3.9
python-bidi 0.4.2
python-dateutil 2.8.2
pytweening 1.0.4
pytz 2022.1
PyWavelets 1.3.0
pywin32 303
pywin32-ctypes 0.2.0
pywinauto 0.6.8
PyYAML 6
requests 2.27.1
scipy 1.8.0
Send2Trash 1.8.0
setuptools 58.1.0
six 1.16.0
SQLAlchemy 1.4.39
sudo 1.0.0
tifffile 2022.3.16
toml 0.10.2
toolz 0.11.2
torch 1.11.0
torchaudio 0.11.0
torchvision 0.12.0
traitlets 5.1.1
typing_extensions 4.1.1
urllib3 1.26.9
wcwidth 0.2.5
webencodings 0.5.1
wrapt 1.13.3

Solution

  • This was an issue with extra python installations on my device.

    I fixed this issue by deleting an extra set of python installations on my computer.

    Create venv without admin access python

    import keyring
    keyring.set_keyring(Windows.WinVaultKeyring())
    keyring.get_password("Service", "Username")
    keyring.set_password("TestService","TestUser", "TestPassword")
    keyring.get_password("TestService", "TestUser")
    

    Prints:

    'TestPassword'
    

    Edit, this also fixes the need to set the default keyring. I can omit: keyring.set_keyring(Windows.WinVaultKeyring())