73

I am trying to connect MongoDB from Atlas.

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

My pymongo version is 3.6.1

I have installed dnspython and done import dns

But i still get this error:

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

2
  • 4
    I would need to rephrase my question. I somehow manage to resolve this myself by restarting the kernel of my jupyter notebook.
    – addicted
    Oct 22, 2018 at 13:49
  • you must use mongo:// instead of mongodb+srv://
    – msklc
    Jul 4, 2022 at 13:13

11 Answers 11

122

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]

And as suggested by @lukrebs, add quotes for ZSH:

pip3 install 'pymongo[srv]'
2
  • 28
    for those who are using macos and zsh and are getting zsh: no matches found: pymongo[srv], may try pip3 install 'pymongo[srv]' Oct 16, 2019 at 22:28
  • What would be the syntax for poetry?
    – Vichoko
    Jul 12, 2022 at 19:48
23

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.

2
  • 3
    the error went away after i did this in command line "pip install dnspython==1.15.0"
    – trillions
    Jul 18, 2019 at 18:51
  • that was my problem! thanks! Jan 17, 2022 at 13:50
19

I solved this problem with:

$ python -m pip install pymongo[srv]

2
  • 1
    For python3 users,the following is useful as well: python3-m pip install pymongo[srv]
    – Erdem Tuna
    Jul 30, 2019 at 13:14
  • 5
    If use zsh, your command must be python3 -m pip install 'pymongo[srv]'.
    – laurajaime
    Aug 12, 2019 at 16:26
11

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

11

I got stuck with the same problem and tried

pip install dnspython==2.0.0

This is the latest version from https://pypi.org/project/dnspython/

It worked :D

2
  • dnspython==2.0.0 also worked for me even though it did not work with pymongo[tls,srv] and dnspython==1.16.0.
    – Joshua M
    Oct 28, 2020 at 20:00
  • You saved me. Thanks.
    – Morton
    Nov 19, 2020 at 5:39
5

you can use mongo:// instead of mongodb+srv://

1
  • I don't think older version of mongo support replicas. If you use mongo://, final URI will be mongo://db-part-0.host.com:27017, db-part-1.host.com:27017, db-part-2.host.com:27017. Yes, there is comma in the URI.
    – addicted
    Sep 29, 2019 at 19:45
0

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)

1
  • 2
    there is no such thing as mongo+srv there is mongodb+srv
    – Naz
    Jul 22, 2019 at 16:41
0

I had the same problem on Ubuntu 18 but since I am using Anaconda I just tried

Conda install dns python

I had an IPython running, it did not work while the same instance was open but when I restarted that instance it worked.

On a different machine using

Conda install dns python

and it worked but I had to restart my machine altogether due to a different reason before testing it

1
  • I think the command should be corrected as conda install dnspython (note that there's no space between dns and python). The library name is dnspython. And, for conda, this is the way to go. There's no installation called pymongo[srv] when you are in conda... Aug 24, 2022 at 20:40
0

I had the same issue and found the following line.

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

It worked for me.

0
0
pip install dnspython

dnspython is a DNS toolkit for Python. It supports almost all record types. It can be used for queries, zone transfers, and dynamic updates. It supports TSIG authenticated messages and EDNS0.

0

None of the existing answers had worked for me. I had to do the following:

sudo apt-get install python3-dnspython

Not the answer you're looking for? Browse other questions tagged or ask your own question.