在python项目中的Pymongo问题

时间:2015-04-29 09:02:51

标签: python pymongo

我已经安装了pymongo,当我开始一个新项目并且我写了 import pymongo Python向我显示下一个错误。

Python版本:3.4.3

错误代码

Traceback (most recent call last):
  File "TestMongoDB.py", line 3, in <module>
    import pymongo
ImportError: No module named 'pymongo'

Pymongo

如果我把这段代码 python -c "import pymongo; print(pymongo.version); print(pymongo.has_c())"进入终端,显示下一条信息。

3.0rc1
True

我正在尝试使用此命令通过终端执行我的python代码

python3 TestMongoDB.py

输入

python -c "import pymongo; import sys; print(pymongo.version); print(pymongo.has_c()); print (sys.path)"

输出

3.0rc1
True
['', '/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg', '/Library/Python/2.7/site-packages/pymongo-3.0rc1-py2.7-macosx-10.10-intel.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

输入python代码

# Include mongo library.
import pymongo

# Include sys library.
import sys

print (sys.path)

输出python代码

['/Users/ismaelmoral/python/TestMongoDB', '/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg', '/Library/Python/2.7/site-packages/pymongo-3.0rc1-py2.7-macosx-10.10-intel.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

1 个答案:

答案 0 :(得分:1)

您似乎已经为Python 2安装了pymongo,但是您尝试使用Python 3运行它。您需要为Python 3安装它。

您可以看到有效的命令使用python,而失败的命令使用python3。您可以通过检查这是否有效来验证这是否是问题:

python testMongoDB.py

然后失败了:

python3 -c "import pymongo; print(pymongo.version); print(pymongo.has_c())"

解决方案

我假设您使用pip来安装软件包。您需要使用pip进行Python 3安装,而不是Python 2默认路径上的安装。

转到目录\root\dir\for\Python3X\Scripts。运行您在那里找到的pip,例如./pip install pymongo并将为Python 3安装它

如果该解决方案无法满足您的需求 - 请查看Pymongo installation docs