1

I am trying to connect to my MongoDB Cluster using the SRV scheme and I am encountering a problem while trying to connect. Below is the code I've tried

from motor import motor_asyncio
motor_client = motor_asyncio.AsyncIOMotorClient('mongodb+srv://<user>:<password>@examplecluster-ece7n.azure.mongodb.net/test?retryWrites=true&w=majority')
_db = self.motor_client.translations
_collection = _db.translations_collections

I have dnspython module installed but this is the error that still pops up:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/libneko/extras/superuser.py", line 176, in execute_in_session
    result = await locals()["aexec"](ctx, ctx.bot)
  File "<string>", line 4, in aexec
  File "/usr/local/lib/python3.7/site-packages/motor/core.py", line 141, in __init__
    delegate = self.__delegate_class__(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 524, in __init__
    res = uri_parser.parse_uri(entity, port, warn=True)
  File "/usr/local/lib/python3.7/site-packages/pymongo/uri_parser.py", line 318, in parse_uri
    raise ConfigurationError('The "dnspython" module must be '
pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs

It should successfully connect to the cluster from what I expect and also, just two points to specify

  1. I installed dnspython AFTER the error popped up.
  2. It seems the problem is similar to this pymongo - mongodb+srv "dnspython must be installed" error but I am NOT using Jupyter Notebook.
Dylee
  • 103
  • 2
  • 12
  • Its probably related to the version confict for `dnspython`. it can be dependency for more than one library and all library support different versions of `dnspython`. I can not recall myself but I certainly faced this issue in past. – Umesh Chaudhary Jul 22 '19 at 08:23

3 Answers3

0

In the MongoClient() URI just specify mongodb instead of mongodb+srv: and just replace the placeholder fields ie password and dbname with your cluster password and databasename Or if above did not work just try to install python -m pip install pymongo[srv] this command and restart whatever IDE you are using Hope this helps

Abhi
  • 101
  • 1
  • 3
0

I had this error today when I hadn't installed dnspython. So I added dnspython==2.3.0 to my requirements.txt and that fixed it. Find the other modules' versions in my requirements.txt as follows:

Flask==2.2.0
pymongo==3.11.3
Werkzeug>=2.2.0
Gunicorn
dnspython==2.3.0

Another thing to note is, it isn't enough for the accessing computer to know the DB username and password. You will have to make the settings in Atlas to allow access to the IP of the computer where you are trying to access Atlas from. To do this, login to Atlas, click on your cluster (cluster name)->Network Access-> ADD IP ADDRESS.

rashid_m
  • 11
  • 2
-1

I had the exact same issue, and turns out one of my file in the project was named dns.py. So Python would use my file instead of the dns library, hence the error. Renaming it fixed the issue.

Hope this helps.

olidoodle
  • 89
  • 1
  • 3