Reputation: 2189
I am having a strange problem when I run a script, I can not load pymongo
, but it is available when I run the hashbang interpreter (/usr/bin/env python
).
The original call import pymongo
was buried quite deep in a class, but even if I call it at line 1, it fails.
Why is this?
Here is some output from the python console showing pymongo
is available:
# /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
>>>
Here are the first three lines of a script
# head -n 3 ./app/app.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymongo
Here is the failure:
# python ./app/app.py
Traceback (most recent call last):
File "./app/app.py", line 3, in <module>
import pymongo
ImportError: No module named pymongo
But running with /usr/bin/env
works okay... why?
# /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
Upvotes: 1
Views: 2696
Reputation: 178
I have the same issue with pyserial, but in my case my script file has name serial.py, so when I try to import serial it can't recognize my package. I just rename my file and works fine.
Upvotes: 3
Reputation: 2189
Running /usr/bin/python
was 2.6.6 and /usr/local/bin/python
was 2.7.4. pip-python
was affecting /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
Both instances must refer to the same libs
. Okay fine...
Upvotes: 0
Reputation: 4860
Check .pythonrc in your home directory. The console executes it (if present), scripts don't. It's the only thing I can think of right now. Maybe it sets sys.path such that pymongo is accessible.
I assume that you're trying to run the script as you, that is, the same user that starts the interactive interpreter. If not, well, that could also be a cause.
Upvotes: 0