Nethrenial
Nethrenial

Reputation: 748

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'


Upvotes: 4

Views: 4924

Answers (4)

toshi456
toshi456

Reputation: 223

Executing pip install dnspython didn't work for me. The following code did the job:

from pymongo import MongoClient
import dns.resolver

dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers=['8.8.8.8']

Upvotes: 1

Pascal_Unique
Pascal_Unique

Reputation: 1

For me uninstall/install pymongo solved the issue (updating did not help):

pip uninstall pymongo
pip install pymongo

Upvotes: 0

Dmitry
Dmitry

Reputation: 737

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.

Upvotes: 6

rohitcoder
rohitcoder

Reputation: 400

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

pip3 install dnspython

Upvotes: 2

Related Questions