Python 无法导入 MongoClient

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17624416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:42:14  来源:igfitidea点击:

Can't import MongoClient

pythonmongodbubuntupython-2.7

提问by l--''''''---------''''''''''''

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 pymongowithout issues.

我能够import pymongo没有问题。

I am running mongodb 2.2.3and Python 2.7.

我正在跑步mongodb 2.2.3Python 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?

我究竟做错了什么?

采纳答案by Chris

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.

该软件包可能已过时或损坏。跑sudo apt-get purge python-pymongo,然后sudo apt-get install python-pip,最后sudo pip install pymongo

回答by Esenti

According to docs, MongoClientwas introduced in version 2.4. As you installed pymongofrom 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):

根据docsMongoClient是在 2.4 版中引入的。当您pymongo从分发存储库安装时,它很可能不是最新版本。尝试通过 PiP 安装它(删除您首先安装的那个):

pip install pymongo

回答by dbarenas

yes it's true you need to update use, check the upgrade section says http://api.mongodb.org/python/current/installation.html

是的,您确实需要更新使用,请检查升级部分说http://api.mongodb.org/python/current/installation.html

   easy_install -U pymongo

回答by Kevin Zhao

If you used pymongo.Connection and encountered error, you should know that after the new updates, you should use pymongo.MongoClient instead. I had this issue so hopefully this will be helpful to someone else.

如果您使用 pymongo.Connection 并遇到错误,您应该知道在新更新后,您应该使用 pymongo.MongoClient 代替。我遇到了这个问题,所以希望这对其他人有帮助。

回答by Parvesh Kumar Tandon

Python Script

Python脚本

Try it once, into python script run following statement

尝试一次,进入python脚本运行以下语句

# try.py

import pymongo

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

如果上面的语句没有抛出任何异常,那么你需要安装pymongo-2.4,如果现有pymongo版本是2.3

Open terminal

打开终端

First you need to uninstall old pymongoversion

首先你需要卸载旧pymongo版本

pip uninstall pymongo

Then use following commands to install pymongo-2.4

然后使用以下命令安装 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

回答by hagay

I would like to suggest more robust answer:

我想建议更可靠的答案:

pip show pymongo

now see where it direct you and from there simply remove the pymongo directory

现在看看它指向你的位置,然后从那里简单地删除 pymongo 目录

rm -rf <the dir where pymongo lay>/pymongo*

Now, you can safely install pymongo again:

现在,您可以再次安全地安装 pymongo:

pip install pymongo

回答by Kavya_D

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.

如果当前工作目录中有多个 .py 程序,则可能会出现此问题。删除它们解决了我的错误。我不确定原因。

回答by Ananda

I had the same problem on Ubuntu 16.04.
This solved my problem:

我在 Ubuntu 16.04 上遇到了同样的问题。
这解决了我的问题:

sudo apt-get install build-essential python-dev

sudo apt-get install python-pip

sudo pip install pymongo

回答by krishna kanth

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

如果您将脚本命名为 pymongo.py,它会屏蔽您要从中导入的 pymongo 模块。

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

将您的脚本重命名为 xyz.py 之类的其他名称(如果旁边创建了 pymongo.pyc 文件,则将其删除)。