4

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'


1
  • Please post your output of pip freeze Commented Dec 29, 2020 at 9:39

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to try this and yeah lot of incompatibility issues. I tried node.js for a while now. I think it's a good fit for me. Anyway, thank you for answering my question, appreciate it a lot.
pip install dnspython==2.1.0 worked for me; I also ignored conflict error.
2

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

pip3 install dnspython

Comments

1

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']

Comments

0

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

pip uninstall pymongo
pip install pymongo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.