Can't import MongoClient

2020-02-27 23:40发布

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?

9条回答
神经病院院长
2楼-- · 2020-02-28 00:35

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.

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-28 00:37

This problem may occur if you have multiple .py programs in the current working directory. Deleting them solved my error. I am not sure of the reason though.

查看更多
我只想做你的唯一
4楼-- · 2020-02-28 00:39

Python Script

Try it once, into python script run following statement

# try.py

import pymongo

If above statement didn't throw any exception then you need to install pymongo-2.4, if existing pymongo verion is 2.3

Open terminal

First you need to uninstall old pymongo version

pip uninstall pymongo

Then use following commands to install pymongo-2.4

wget http://pypi.python.org/packages/source/p/pymongo/pymongo-2.4.tar.gz
tar xzf pymongo-2.4.tar.gz
cd pymongo-2.4
python setup.py install
查看更多
登录 后发表回答