Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo import Connection - causes ImportError

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.

like image 757
Marcel Kalveram Avatar asked Dec 08 '22 20:12

Marcel Kalveram


2 Answers

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.

like image 113
Bram Avatar answered Dec 12 '22 12:12

Bram


Use :

from flask.ext.pymongo import MongoClient

as :

from pymongo import Connection

is deprecated.

MongoClient works almost same as Connection.

like image 25
Raj Damani Avatar answered Dec 12 '22 12:12

Raj Damani