无头Ubuntu系统中Python keyring存储密码失败问题求助
无头Ubuntu下keyring.set_password报错的解决办法
问题背景
开发需要登录凭证的Python脚本时,keyring库在无头Ubuntu系统执行keyring.set_password报错,核心错误是Prompt dismissed.——带GUI的系统会弹出创建密钥集合的提示,但无头环境无法交互,导致创建失败。
报错信息:
Python 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import keyring >>> keyring.set_password("test_app", "testuser", "testpassword") Traceback (most recent call last): File "/usr/local/lib/python3.12/dist-packages/secretstorage/util.py", line 48, in send_and_get_reply raise DBusErrorResponse(resp_msg) jeepney.wrappers.DBusErrorResponse: [org.freedesktop.DBus.Error.UnknownMethod] ('Object does not exist at path “/org/freedesktop/secrets/collection/login”',) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/lib/python3.12/dist-packages/secretstorage/collection.py", line 177, in get_default_collection return Collection(connection) ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/dist-packages/secretstorage/collection.py", line 45, in __init__ self._collection.get_property('Label') File "/usr/local/lib/python3.12/dist-packages/secretstorage/util.py", line 67, in get_property (signature, value), = self.send_and_get_reply(msg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/dist-packages/secretstorage/util.py", line 52, in send_and_get_reply raise ItemNotFoundException('Item does not exist!') from resp secretstorage.exceptions.ItemNotFoundException: Item does not exist! During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/keyring/backends/SecretService.py", line 61, in get_preferred_collection collection = secretstorage.get_default_collection(bus) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/dist-packages/secretstorage/collection.py", line 179, in get_default_collection return create_collection(connection, 'Default', 'default', session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/dist-packages/secretstorage/collection.py", line 159, in create_collection raise PromptDismissedException('Prompt dismissed.') secretstorage.exceptions.PromptDismissedException: Prompt dismissed. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/keyring/core.py", line 61, in set_password get_keyring().set_password(service_name, username, password) File "/usr/lib/python3/dist-packages/keyring/backends/chainer.py", line 56, in set_password return keyring.set_password(service, username, password) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/keyring/backends/SecretService.py", line 87, in set_password collection = self.get_preferred_collection() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/keyring/backends/SecretService.py", line 63, in get_preferred_collection raise InitError("Failed to create the collection: %s." % e) keyring.errors.InitError: Failed to create the collection: Prompt dismissed.. >>>
解决方法
方法1:切换到文件存储后端
跳过SecretService的GUI依赖,使用文件后端存储密码:
- 明文存储(适合测试环境):
在脚本开头添加:import keyring from keyring.backends.file import PlaintextKeyring keyring.set_keyring(PlaintextKeyring()) # 之后正常调用keyring.set_password即可 keyring.set_password("test_app", "testuser", "testpassword") - 加密存储(推荐生产环境):
先安装依赖:
脚本中配置加密后端:pip install cryptographyimport keyring from keyring.backends.cryptfile import CryptFileKeyring kr = CryptFileKeyring() # 首次使用会提示设置加密密码,后续操作需要验证此密码 kr.set_password("test_app", "testuser", "testpassword") keyring.set_keyring(kr)
方法2:配置SecretService在无头环境工作
如果必须使用系统密钥环,手动创建默认集合:
- 安装必要工具:
sudo apt update && sudo apt install gnome-keyring dbus-x11 - 启动dbus会话和keyring守护进程:
export DBUS_SESSION_BUS_ADDRESS=$(dbus-daemon --session --fork --print-address) gnome-keyring-daemon --start --components=secrets - 创建默认密钥集合:
执行后输入集合密码,完成创建。之后运行Python脚本即可正常使用keyring。secret-tool create --label='Default' collection default
方法3:通过环境变量指定后端
无需修改代码,直接通过环境变量强制使用文件后端:
export PYTHON_KEYRING_BACKEND=keyring.backends.file.PlaintextKeyring # 或者加密后端: # export PYTHON_KEYRING_BACKEND=keyring.backends.cryptfile.CryptFileKeyring
然后运行你的Python脚本即可。
内容的提问来源于stack exchange,提问作者Brian
相关产品推荐
相关产品推荐

