Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration Error when trying to connect mongodb database in python

I keep getting pymongo.errors.ConfigurationError: query() got an unexpected keyword argument 'lifetime' even though I have all the latest versions of pymongo and dnspython installed. This is my code...

import pymongo

client = pymongo.MongoClient("mongodb+srv://Nethrenial:[email protected]/TooDooo?retryWrites=true&w=majority")
users = client.users.find()
user_list = []
for user in users:
    user_list.append(user)
print(user_list)

And this is the complete exception,

Traceback (most recent call last):
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\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 "C:\Users\lenovo\Desktop\TooDooo\database_functions.py", line 4, in <module>
    client = pymongo.MongoClient("mongodb+srv://Nethrenial:[email protected]/TooDooo?retryWrites=true&w=majority")
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\mongo_client.py", line 639, in __init__
    res = uri_parser.parse_uri(
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\uri_parser.py", line 500, in parse_uri
    nodes = dns_resolver.get_hosts()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\srv_resolver.py", line 102, in get_hosts
    _, nodes = self._get_srv_response_and_hosts(True)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\srv_resolver.py", line 83, in _get_srv_response_and_hosts
    results = self._resolve_uri(encapsulate_errors)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\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'


like image 818
Nethrenial Avatar asked Oct 15 '25 13:10

Nethrenial


2 Answers

If you use Python 3, you must use dnspython3 instead of dnspython. This lib actually uses dnspython under the hood, but installs an incorrect version, 1.15.0, the same as for main library. You must change it manually to 2.1.0. On PC (Windows) it also installs starlette lib. Never change it's version to upgrade, it stops working.

Incompatibility issues and poor ecosystem support seems to be a common problem in the Python world.

like image 132
Dmitry Avatar answered Oct 17 '25 14:10

Dmitry


In Most cases, this occurs because of outdated dnspython lib. I solved my issue just by re-installing / updating dnspython

pip3 install dnspython
like image 29
rohitcoder Avatar answered Oct 17 '25 14:10

rohitcoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!