pymongo - mongodb+srv "dnspython must be installed" error

pymongo - mongodb+srv "dnspython must be installed" error

pymongo find
pymongo aggregate
pymongo install
pymongo mongoclient
pymongo github
pymongo objectid
flask-pymongo
pymongo query

I am trying to connect MongoDB from Atlas But I encountered:

dnspython must be installed error

My mongo uri (mockup): mongodb+srv://abc:123@something.something.com/admin?retryWrites=True

My pymongo version: 3.6.1 I have installed dnspython and done import dns

Still, I am getting the error:

dnspython module must be installed to use mongodb+srv:// URI


In order to use mongo+srv protocol, you need to install pymongo-srv Launch this command to do it with python 3:

pip3 install pymongo[srv]

or this one for other versions:

pip install pymongo[srv]

Tutorial, find_one()) {u'_id': ObjectId(''), u'author': u'Mike', u'date': datetime.datetime(), u'​tags': [u'mongodb', u'python', u'pymongo']  pymongo – Python driver for MongoDB¶ Python driver for MongoDB. pymongo.version = '3.9.0'¶ str(object=’’) -> string. Return a nice string representation of the object. If the argument is a string, the return value is the same object. pymongo.MongoClient¶ Alias for pymongo.mongo_client.MongoClient. pymongo.MongoReplicaSetClient¶


I solved this problem with:

$ python -m pip install pymongo[srv]

PyMongo, Documents¶. Data in MongoDB is represented (and stored) using JSON-style documents. In PyMongo we use dictionaries to represent documents. As an example  MongoDB stores data in BSON format. BSON strings are UTF-8 encoded so PyMongo must ensure that any strings it stores contain only valid UTF-8 data. Regular strings (<type ‘str’>) are validated and stored unaltered. Unicode strings (<type ‘unicode’>) are encoded UTF-8 first.


I would like to answer my own questions here. As I mentioned in the comment, the kernel of the jupyter notebook has to be restarted in order for the pymongo to take effect of the loaded dnspython.

pymongo · PyPI, PyMongo. Python needs a MongoDB driver to access the MongoDB database. In this tutorial we will use the MongoDB driver "PyMongo". We recommend that  PyMongo tutorial shows how to program MongoDB in Python. MongoDB is a NoSQL cross-platform document-oriented database.


In requirements.txt, replace pymongo with pymongo[tls,srv], as mentioned here.

PyMongo 3.10.1 documentation, The PyMongo distribution contains tools for interacting with MongoDB database from Python. The bson package is an implementation of the BSON format for  PyMongo. If you want to work with MongoDB databases using Python, PyMongo is the tool you will need. This section presents the basic CRUD commands you will often use in order to manipulate the database. Mongo Shell vs PyMongo. Note that there are two ways to access a MongoDB database: Using the Mongo Shell; Through PyMongo


May be the protocol, your URI should start with:

mongo+srv instead of mongo+src

If it still not working please put a pip list with the versions of PyMongo and dnspython (and version of python that you are using)

Tutorial, This article looks at how to use Python to interface with MongoDB along with an overview of SQL vs. NoSQL, PyMongo, and MongoEngine. PyMongo If you want to work with MongoDB databases using Python, PyMongo is the tool you will need. This section presents the basic CRUD commands you will often use in order to manipulate the database.


Python MongoDB, PyMongo. Python needs a MongoDB driver to access the MongoDB database. In this tutorial we will use the MongoDB driver "PyMongo". We recommend that  PyMongois a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python. This documentation attempts to explain everything you need to know to use PyMongo.


mongodb/mongo-python-driver: PyMongo - the Python , © MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.


Introduction to MongoDB and Python – Real Python, The PyMongo distribution contains tools for interacting with MongoDB database from Python. The bson package is an implementation of the BSON format for Python. The pymongo package is a native Python driver for MongoDB. The gridfs package is a gridfs implementation on top of pymongo.