I'm a newbie to python (about one week in), so maybe I'm just missing something obvious...
I have been unable to import and use a module in my script code.py
. The module was installed using easy_install and is called googlemaps. I installed it (successfully) with the command:
sudo easy_install googlemaps
When I try to import the module from the Python interpreter, it seems to work fine:
>>> googlemaps
<module 'googlemaps' from '/usr/local/lib/python2.7/dist-packages/googlemaps-1.0.2-py2.7.egg/googlemaps.pyc'>
However, when I try to do the same in a script, it gives the following traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 239, in process
return self.handle()
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 420, in _delegate
return handle_class(cls)
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 396, in handle_class
return tocall(*args)
File "/var/www/example.com/application/code.py", line 57, in GET
self.generate_map()
File "/var/www/example.com/application/code.py", line 64, in generate_map
from googlemaps import GoogleMaps
ImportError: No module named googlemaps
I suspect that this is some kind of path issue, but I don't fully understand why or how to fix it. If I issue the following from the interpreter:
>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/googlemaps-1.0.2-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
everything looks fine, but if I do the same thing from a script, the result is missing the google-maps-1.0.2-py2.7.egg entry:
['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
Do I have to manually add the path when using from a script, or what am I missing here??? I haven't had this problem with other installed modules.
Per Burhan Khalid's suggestion, my problem had to do with the fact that the script code.py
was being run by an apache webserver using the mod_wsgi
module. It apparently didn't incorporate changes to the sys.path until after running:
sudo service apache2 reload
You if you are using mod_wsgi and have similar problems, give it a try!