我有一个奇怪的问题,当我运行一个脚本,我不能加载pymongo
,但它是可用的,当我运行哈希邦解释器(/usr/bin/env python
)。
原来的调用import pymongo
隐藏在一个类的深处,但是即使我在第1行调用它,它也失败了。
为什么会这样?
下面是python控制台显示pymongo
可用的一些输出:
# /usr/bin/env python
Python 2.7.4 (default, Apr 23 2013, 14:02:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>>
以下是脚本的前三行
# head -n 3 ./app/app.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymongo
下面是失败:
# python ./app/app.py
Traceback (most recent call last):
File "./app/app.py", line 3, in <module>
import pymongo
ImportError: No module named pymongo
但是用/usr/bin/env
运行是可以的…为什么?
# /usr/bin/env python ./app/app.py
Traceback (most recent call last):
File "./app/app.py", line 37, in <module>
db = backend.flowbackend.getBackendObject(config.db_backend, config.db_host, config.db_port, config.db_user, config.db_password, config.db_name)
File "./app/../lib/backend/flowbackend.py", line 268, in getBackendObject
return MongoBackend(host, port, user, password, databaseName)
File "./app/../lib/backend/mongobackend.py", line 13, in __init__
self.connect()
File "./app/../lib/backend/mongobackend.py", line 62, in connect
self.conn = pymongo.Connection(self.host, self.port)
File "/usr/local/lib/python2.7/site-packages/pymongo/connection.py", line 220, in __init__
max_pool_size, document_class, tz_aware, _connect, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 336, in __init__
raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: could not connect to 127.0.0.1:27017: [Errno 111] Connection refused
我对pyserial也有同样的问题,但在我的情况下,我的脚本文件名为serial.py,所以当我尝试导入serial时,它无法识别我的包。我只是重命名我的文件,工作正常
检查主目录中的.pythonrc。控制台执行它(如果有的话),脚本不执行。这是我现在唯一能想到的。也许它设置了sys。pymongo可访问的路径。
我假设您尝试以您的身份运行脚本,也就是说,以启动交互式解释器的同一用户。如果不是,那也可能是原因。
运行/usr/bin/python
为2.6.6,/usr/local/bin/python
为2.7.4。pip-python
正在影响/usr/bin/python
。
# pip-python -E /usr/local/bin/python freeze
distribute==0.6.10
iniparse==0.3.1
pycurl==7.19.0
pygpgme==0.1
urlgrabber==3.9.1
wsgiref==0.1.2
yum-metadata-parser==1.1.2
# pip-python install pymongo
...
# pip-python -E /usr/local/bin/python freeze
distribute==0.6.10
iniparse==0.3.1
pycurl==7.19.0
pygpgme==0.1
pymongo==2.5
urlgrabber==3.9.1
wsgiref==0.1.2
yum-metadata-parser==1.1.2
# pip-python -E /usr/bin/python freeze
distribute==0.6.10
iniparse==0.3.1
pycurl==7.19.0
pygpgme==0.1
pymongo==2.5
urlgrabber==3.9.1
yum-metadata-parser==1.1.2
两个实例必须引用同一个libs
。好细…