关于mongodb:import pymongo在Python解释器中工作,但不在脚本中工作

import pymongo works in Python interpreter but not script

我第一次安装pymongo时使用的是easy-install,但没有成功,所以我尝试使用pip,但它仍然失败。

这在终点站很好:

1
2
3
4
5
6
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行

1
import pymongo

引发以下错误:

File"test.py", line 10, >in
import pymongo
ImportError: No module named pymongo

我使用的是Apache和Python的标准lion版本。还有其他人经历过吗?

谢谢

编辑:我还应该提到,在安装过程中,它抛出了以下错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.
**************************************************************

然后继续说

1
2
3
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/plat darwin","/system/library/python.7/plat darwin","/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/lib/python2.7/lib tk","/system/library/frameworks/python2.7/lib tk","/orks/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/formcode-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",/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/python2.7","/library/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","/library/frameworks/python.framework/versions/2.7/lib/python2.7/lib old","/library/frameworks/python.framework/versions/2.7/lib old","meworks/python.framework/versions/2.7/lib/python2.7/lib dynload","/library/frameworks/python.framework/versions/2.7/lib/python2.7/site packages","/library/python/2.7/site packages']


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

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

理想情况下,希望找到一种方法永久地将此附加到pythonpath,但目前这是可行的!


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

rhel/centos:sudo yum安装gcc python develdebian/ubuntu:sudo-apt-get-install-gcc-python-dev

然后再次尝试安装pymongo。