Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve a python module import collision?

Tags:

python

This is similar to this question, but that one doesn't propose a solution.

I have ~/google_appengine/google/appengine and i have /usr/lib64/python2-7/site-packages/google/protobuf.

I can't "import google.appengine" since the other google folder gets in the way. How do i fix this?

Here's a minimal example and error:

$ python
Python 2.7.3 (default, Jun 12 2012, 13:50:02) 
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.appengine
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named appengine

As requested, here's my sys.path:

['', '/usr/lib64/python2.7/site-packages/nose-1.1.2-py2.7.egg', '/usr/lib64/python2.7/site-packages/mock-0.8.0-py2.7.egg', '/usr/lib64/python2.7/site-packages/coverage-3.5.1-py2.7-linux-x86_64.egg', '/usr/lib64/python2.7/site-packages/rosinstall-0.6.11-py2.7.egg', '/usr/lib64/python2.7/site-packages/vcstools-0.1.12-py2.7.egg', '/home/murph/google_appengine', '/home/murph/google_appengine/google/appengine', '/home/murph/google_appengine/google', '/home/murph/ros/ros/core/roslib/src', '/usr/lib/portage/pym', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode', '/usr/lib64/portage/pym']

And this showing which 'google' it's importing:

$ python
Python 2.7.3 (default, Jun 12 2012, 13:50:02) 
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google
>>> print google.__path__
['/usr/lib64/python2.7/site-packages/google']
like image 961
Murph Avatar asked Feb 19 '23 08:02

Murph


1 Answers

There is a change in the import mecanism since the 2.5 version ( see this PEP ) :

import google will be for modules in the standard library

For relative

from __future__ import absolute_import
from .google import appengine # (or  from google import appengine)

This blog entry explain how to import a relative module.

like image 173
lucasg Avatar answered Feb 21 '23 00:02

lucasg