pythonwindowspython-keyring

Python Keyring does not overwrite entry but creates new entry


I'm having trouble with the python keyring library. I want to extract passwords from an entry in the windows credential manager. After that I want to overwrite the password of that specific entry. Here is my Code.

import keyring

# The service name in the credentials manager is "User@Service", the username is "User"
password = keyring.get_password("User@Service", "User")

# Overwrite the password
keyring.set_password("User@Service", "User", "NewPassword")

I would expect this to overwrite my old password, but it creates a new entry in the credentials manager with the service name "User@User@Service"

I also tried to simpley do keyring.set_password("Service", "User", "NewPassword") but that creates a new entry with the service name "Service". I simply can't get it to overwrite an existing entry. Any help is appreciated. Thanks.


Solution

  • I ran into this problem today as well. Seems like the same thing mentioned here:

    https://github.com/jaraco/keyring/issues/545

    I don't know if it was ever fixed or has been an issue since then. I can't get it to work, as it just creates a new credential unless both versions already exist (service and user@service).

    I am working around this by just deleting the credential and creating a new one.

    import keyring
    from keyring.errors import PasswordDeleteError
    
    try:
        keyring.delete_password("testapp", "username")
    except PasswordDeleteError:
        # In case the password doesn't exist yet
        pass
    keyring.set_password("testapp", "username", "pw")