Search code examples
python-3.xdjangomongodbpymongo

pymongo fails with error 'sys' is not defined


I have a Django app that queries mongodb.

requirements.txt:

# other dependencies
pymongo==4.1.0

View:

client = pymongo.MongoClient("mongodb+srv://*****@****.mongodb.net/test_db?retryWrites=true&w=majority")

When this line is executed, the Django server throws the following error:

venv/lib/python3.9/site-packages/pymongo/uri_parser.py, line 467, in parse_uri 
Exception: name 'sys' is not defined

How do I fix this error? I'm on Python3.9 and latest Django.


Solution

  • Support for mongodb+srv:// URIs requires dnspython:

    use pip install pymongo[srv]