gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:10 27 4
gpt4 key购买 nike

这类似于 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']

最佳答案

自 2.5 版本以来,导入机制发生了变化(参见 this PEP):

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

亲戚

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

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

关于python - 如何解决 python 模块导入冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13613889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com