PyMongo import join - raises ImportError

I am invoking the following simple script to connect to a mongo database through Python.

This is an example from the 10gen training course M101 - MongoDB for developers , and according to the forums, I am not the only person who has this problem.

import pymongo from pymongo import Connection connection = Connection('localhost', 27017) 

I installed pymongo with pip as described here and everything worked fine. Now when I try to import the Connection class, it throws the following error:

 ImportError: cannot import name Connection 

I reviewed the following thread: pymongo is installed, but the import is complete

but this does not apply to my environment, since I am using Python 2.7.1, and I'm on Mac OS instead of CentOS. I also did some research on Google, but the only thing that I have found out so far is to verify that PyMongo was installed correctly, which I did and returns:

 Requirement already satisfied 

Any help would be appreciated to move on during the course. Thanks in advance.

+4
source share
2 answers

Make sure there are no files named pymongo.py or pymongo.pyc in the path from which the script is pymongo.pyc . I called my test script pymongo.py , which made Python try and import Connection from the same file. Renaming it to pymongo-test.py and deleting the automatically generated pymongo.pyc solved the problem.

+13
source

Using:

 from flask.ext.pymongo import MongoClient 

as:

 from pymongo import Connection 

outdated.

MongoClient works almost the same as Connection.

+1
source

Source: https://habr.com/ru/post/1442185/


All Articles