-1

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.

robguinness
  • 16,266
  • 14
  • 55
  • 65

4 Answers4

0

Before using functions from a module, you have to import it using something like:

import googlemaps

to use functions in module googlemaps as googlemaps.FUNC_NAME()

import googlemaps as gmaps

to use functions in module googlemaps as gmaps.FUNC_NAME()

from googlemaps import *

to import the entire namespace and use functions directly as FUNC_NAME(). Be careful about shading other existing names with this option.

See https://docs.python.org/2/tutorial/modules.html for further reference about Python modules.

francois
  • 81
  • 5
0

this seems to work for me but you should try something like this:-

import googlemaps
from googlemaps import GoogleMaps
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
0

Yes, you'd need to append the sys.path to include the needed path. It's really strange that you have a different sys.path in the interpreter and in the script, but I can't help you with that, as I have barely have an inkling how to deal with that on Windows, and I'm a stranger to Linux.

So yes, going :

import sys
sys.path.appen(r"/usr/local/lib/python2.7/dist-packages/googlemaps-1.0.2-py2.7.egg")

Before importing googlemaps allows you to work around the problem, even if it's messy and there is certainly a better solution. Still, it works and you can implement it right now.

inirlan
  • 18
  • 1
  • 8
0

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!

robguinness
  • 16,266
  • 14
  • 55
  • 65