如何解决python模块导入冲突?

时间:2012-11-28 20:33:11

标签: python

这类似于this question,但那个没有提出解决方案。

我有〜/ google_appengine / google / appengine,我有/ usr / lib64 / python2-7 / site-packages / google / protobuf。

我无法“导入google.appengine”,因为其他google文件夹会阻碍。我该如何解决这个问题?

这是一个最小的例子和错误:

$ 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

根据要求,这是我的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']

这显示了'google'导入的内容:

$ 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']

1 个答案:

答案 0 :(得分:7)

自2.5版本以来,导入机制有所变化(见this PEP):

import google将用于标准库中的模块

对于亲戚

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

This blog entry解释了如何导入相关模块。