import pymongo 适用于 Python 解释器,但不适用于脚本

Posted

技术标签:

【中文标题】import pymongo 适用于 Python 解释器,但不适用于脚本【英文标题】:import pymongo works in Python interpreter but not script 【发布时间】:2012-05-01 08:25:40 【问题描述】:

我首先使用 easy_install 安装了 pymongo,但没有成功,所以我尝试使用 pip,但仍然失败。

这在终端很好:

Macintosh:etc me$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 14:13:39) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> 

但是在我脚本的第 10 行

import pymongo

抛出以下错误:

文件“test.py”,第 10 行,>在 导入pymongo ImportError: 没有名为 pymongo 的模块

我正在使用 Apache 和 Python 的标准 Lion 版本。有没有其他人经历过这种情况?

谢谢

编辑:我还应该提到,在安装过程中它会引发以下错误

Downloading/unpacking pymongo
Downloading pymongo-2.1.1.tar.gz (199Kb): 199Kb downloaded
Running setup.py egg_info for package pymongo

Installing collected packages: pymongo
Running setup.py install for pymongo
building 'bson._cbson' extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -Ibson -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c bson/_cbsonmodule.c -o build/temp.macosx-10.3-fat-2.7/bson/_cbsonmodule.o
unable to execute gcc-4.0: No such file or directory
command 'gcc-4.0' failed with exit status 1

**************************************************************
WARNING: The bson._cbson extension module could not
be compiled. No C extensions are essential for PyMongo to run,
although they do result in significant speed improvements.

If you are seeing this message on Linux you probably need to
install GCC and/or the Python development package for your
version of Python. Python development package names for popular
Linux distributions include:

RHEL/CentOS: python-devel
Debian/Ubuntu: python-dev

Above is the ouput showing how the compilation failed.
**************************************************************

building 'pymongo._cmessage' extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -Ibson -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c pymongo/_cmessagemodule.c -o build/temp.macosx-10.3-fat-2.7/pymongo/_cmessagemodule.o
unable to execute gcc-4.0: No such file or directory
command 'gcc-4.0' failed with exit status 1

**************************************************************
WARNING: The pymongo._cmessage extension module could not
be compiled. No C extensions are essential for PyMongo to run,
although they do result in significant speed improvements.

If you are seeing this message on Linux you probably need to
install GCC and/or the Python development package for your
version of Python. Python development package names for popular
Linux distributions include:

RHEL/CentOS: python-devel
Debian/Ubuntu: python-dev

Above is the ouput showing how the compilation failed.
**************************************************************

然后继续说

Successfully installed pymongo
Cleaning up...
Macintosh:etc me$  

很奇怪。

脚本中的 sys.path 返回为:

['/Library/WebServer/Documents/', '/Library/Python/2.7/site-packages/tweepy-1.7.1-py2.7.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']

在解释器中:

['', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLObject-1.2.1-py2.7.egg', '/Library/Frameworks/ Python.framework/Versions/2.7/lib/python2.7/site-packages/FormEncode-1.2.4-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /site-packages/setuptools-0.6c12dev_r88846-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.1-py2.7.egg ', '/Library/Python/2.7/site-packages/tweepy-1.7.1-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/库/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/ Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/库/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/版本/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/版本/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

【问题讨论】:

也许你的 python 路径在解释器和脚本中不同?在这两种情况下,您的 sys.path 是什么? 嗨,Michal,我已经用两个输出编辑了我的帖子。谢谢你的时间:) 【参考方案1】:

我不确定为什么最后一条消息说“成功安装 pymongo”,但它显然失败了,因为您的系统上没有安装 gcc。您需要执行以下操作:

RHEL/Centos: sudo yum install gcc python-devel Debian/Ubuntu:sudo apt-get install gcc python-dev

然后尝试再次安装 pymongo。

【讨论】:

【参考方案2】:

找到了!在导入 pymongo 模块之前需要附加路径

sys.path.append('/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages')
import pymongo

理想情况下,希望找到一种方法将其永久附加到 pythonpath,但现在可行!

【讨论】:

以上是关于import pymongo 适用于 Python 解释器,但不适用于脚本的主要内容,如果未能解决你的问题,请参考以下文章

PyMongo - 使用变量来 sql 'LIKE' 查询

import pymongo 导致“ImportError: cannot import name BSON”。你如何修复导入错误?

PyMongo基本使用

python—连接mongodb

如何优化 pymongo 中的更新查询以进行抓取项目

[Python3网络爬虫开发实战] 1.5.2-PyMongo的安装