Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import MongoClient

I am unable to do this:

from pymongo import MongoClient

I get:

>>> import pymongo
>>> from pymongo import MongoClient
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name MongoClient
>>> 

I am able to import pymongo without issues.

I am running mongodb 2.2.3 and Python 2.7.

I've also tried this:

>>> connection = pymongo.MongoClient()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MongoClient'
>>> 

What am I doing wrong?

like image 540
Alex Gordon Avatar asked Jul 12 '13 21:07

Alex Gordon


3 Answers

That package is probably outdated or broken. Run sudo apt-get purge python-pymongo, then sudo apt-get install python-pip, then finally sudo pip install pymongo.

like image 117
Chris Avatar answered Nov 07 '22 17:11

Chris


According to docs, MongoClient was introduced in version 2.4. As you installed pymongo from your distribution repository, it's quite possible it's not the most recent version. Try installing it via PiP (remove the one you have installed first):

pip install pymongo
like image 6
Esenti Avatar answered Nov 07 '22 17:11

Esenti


If you had named your script pymongo.py, which masks the pymongo module from which you are importing.

Rename your script to something else like xyz.py (and delete the pymongo.pyc file if one was created next to it).

like image 4
IKriKan Avatar answered Nov 07 '22 16:11

IKriKan