简体   繁体   English

pymongo.errors.ConfigurationError: query() got an unexpected keyword argument 'lifetime'

[英]pymongo.errors.ConfigurationError: query() got an unexpected keyword argument 'lifetime'

import pymongo
from pymongo import MongoClient

cluster = MongoClient('mongodb+srv://test:*****@cluster0.jyuin.mongodb.net/baka?retryWrites=true&w=majority')

db = cluster['baka']
collection = db['test']

post = {'id_': 0, 'name':'nksama'}

collection.insert_one(post)

there is nothing wrong this code right?这段代码没有错吧?

Traceback (most recent call last):                                                                                                                                                  
  File "C:\Users\HP\AppData\Roaming\Python\Python39\site-packages\pymongo\srv_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 "D:\python codes\db\test\dbb.py", line 4, in <module>                                                                                                                        
    cluster = MongoClient('mongodb+srv://nksama:nksama@cluster0.jyuin.mongodb.net/baka?retryWrites=true&w=majority')                                                                
  File "C:\Users\HP\AppData\Roaming\Python\Python39\site-packages\pymongo\mongo_client.py", line 639, in __init__                                                                   
    res = uri_parser.parse_uri(                                                                                                                                                     
  File "C:\Users\HP\AppData\Roaming\Python\Python39\site-packages\pymongo\uri_parser.py", line 500, in parse_uri                                                                    
    nodes = dns_resolver.get_hosts()                                                                                                                                                
  File "C:\Users\HP\AppData\Roaming\Python\Python39\site-packages\pymongo\srv_resolver.py", line 102, in get_hosts                                                                  
    _, nodes = self._get_srv_response_and_hosts(True)                                                                                                                               
  File "C:\Users\HP\AppData\Roaming\Python\Python39\site-packages\pymongo\srv_resolver.py", line 83, in _get_srv_response_and_hosts                                                 
    results = self._resolve_uri(encapsulate_errors)                                                                                                                                 
  File "C:\Users\HP\AppData\Roaming\Python\Python39\site-packages\pymongo\srv_resolver.py", line 79, in _resolve_uri                                                                
    raise ConfigurationError(str(exc))                                                                                                                                              
pymongo.errors.ConfigurationError: query() got an unexpected keyword argument 'lifetime'   

In Most cases, this occurs because of outdated dnspython lib.在大多数情况下,这是由于过时的 dnspython 库造成的。 I solved my issue just by re-installing / updating dnspython我通过重新安装/更新 dnspython 解决了我的问题

pip3 install dnspython

Uninstall pymongo and install once again卸载pymongo并重新安装

pip uninstall pymongo
pip install pymongo[srv]

Install dnspython安装 dnspython

pip install dnspython3

This should work这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM