Getting error when using pymongo

Hi!
I am getting the following error when trying to connect mongodb using pymongo. May I ask if there is anything wrong I did?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jduan/.local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 704, in __init__
    res = uri_parser.parse_uri(
  File "/home/jduan/.local/lib/python3.8/site-packages/pymongo/uri_parser.py", line 467, in parse_uri
    python_path = sys.executable or "python"
NameError: name 'sys' is not defined

Code used

import pymongo
client = pymongo.MongoClient("mongodb+srv://dbAccess:<password>@cluster0.xi9xb.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = client.test

Version:
python 3.8.10
pymongo 4.10.0

You have to replace <password> with the real password of user dbAccess.

2 Likes

Hi @Javen_N_A welcome to the community!

In addition to what @steevej said about the password, this message caught my eye:

NameError: name 'sys' is not defined

sys is a standard Python package. It’s curious if Pymongo cannot find it, as this implies that there’s something wrong with your Python or Pymongo installation.

How did you install Python, and how did you install Pymongo?

Best regards
Kevin

3 Likes

I ran into the same error few days ago. For me the Problem was based on the “dnspython” Module. Try install pymongo with "python -m pip install pymongo[srv]. This is also mentioned inside the “uri_parser.py” File (see Screenshot)

You also have to import sys inside your python File.

Unbenannt

Hope this helps.

3 Likes

Ran into the same issue. I was running pymongo 4.1.0. To resolve it, I downgraded to pymongo 3.12.3. This also meant I had to install dnspython the newer version of pymongo takes care of I believe.

Thank You so much. That works for me. I`m using googlecolab. Tried this and worked for me:

!python -m pip install pymongo==3.12.3
import pymongo