尝试在python中连接mongodb数据库时出现配置错误
pymongo.errors.ConfigurationError: query() got an unexpected keyword argument 'lifetime'
即使我已经安装了所有最新版本的 pymongo 和 dnspython,我仍然会收到
。这是我的代码...
import pymongo
client = pymongo.MongoClient("mongodb+srv://Nethrenial:notrealpassword@db-for-toodooo.shhiq.mongodb.net/TooDooo?retryWrites=true&w=majority")
users = client.users.find()
user_list = []
for user in users:
user_list.append(user)
print(user_list)
这是完全的例外,
Traceback (most recent call last):
File "C:UserslenovoAppDataLocalProgramsPythonPython39libsite-packagespymongosrv_resolver.py", line 72, in _resolve_uri
results = resolver.query('_mongodb._tcp.' + self.__fqdn, 'SRV',
TypeError: query() got an unexpected keyword argument 'lifetime'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UserslenovoDesktopTooDooodatabase_functions.py", line 4, in <module>
client = pymongo.MongoClient("mongodb+srv://Nethrenial:nethrenial@db-for-toodooo.shhiq.mongodb.net/TooDooo?retryWrites=true&w=majority")
File "C:UserslenovoAppDataLocalProgramsPythonPython39libsite-packagespymongomongo_client.py", line 639, in __init__
res = uri_parser.parse_uri(
File "C:UserslenovoAppDataLocalProgramsPythonPython39libsite-packagespymongouri_parser.py", line 500, in parse_uri
nodes = dns_resolver.get_hosts()
File "C:UserslenovoAppDataLocalProgramsPythonPython39libsite-packagespymongosrv_resolver.py", line 102, in get_hosts
_, nodes = self._get_srv_response_and_hosts(True)
File "C:UserslenovoAppDataLocalProgramsPythonPython39libsite-packagespymongosrv_resolver.py", line 83, in _get_srv_response_and_hosts
results = self._resolve_uri(encapsulate_errors)
File "C:UserslenovoAppDataLocalProgramsPythonPython39libsite-packagespymongosrv_resolver.py", line 79, in _resolve_uri
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: query() got an unexpected keyword argument 'lifetime'
回答
如果您使用 Python 3,则必须使用dnspython3代替dnspython. 这个库实际上dnspython在幕后使用,但安装了一个不正确的版本,1.15.0,与主库相同。您必须手动将其更改为 2.1.0。在 PC (Windows) 上,它还安装starlettelib。永远不要改变它的版本来升级,它会停止工作。
不兼容问题和糟糕的生态系统支持似乎是 Python 世界中的普遍问题。