I'm calling the following simple script to connect to a mongo database via Python.
This is an example from the 10gen education course M101 - MongoDB for Developers, and according to the forums I'm not the only person who has this issue.
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 gives me the following error:
ImportError: cannot import name Connection
I have looked at the following thread so far: pymongo installed but import fails
but it doesn't apply to my environment as I'm using Python 2.7.1 and I'm on Mac OS instead of CentOS. I've also done some research on Google but the only thing I found out so far is to verify that PyMongo is installed correctly, which I've done and returns:
Requirement already satisfied
Any help would be appreciated in order to move on in the course. Thanks in advance.
Make sure there are no files called pymongo.py
or pymongo.pyc
in the path you are executing the script from. I named my test script pymongo.py
, which caused Python to try and import Connection from that same file. Renaming it to pymongo-test.py
and removing the automatically created pymongo.pyc
solved the issue.
Use :
from flask.ext.pymongo import MongoClient
as :
from pymongo import Connection
is deprecated.
MongoClient works almost same as Connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With