python-3.xmongodbpymongopython-asynciopymongo-3.x

pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs


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.

Solution

  • 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.