python相对导入示例代码不起作用 [英] python relative import example code does not work

查看:149
本文介绍了python相对导入示例代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在Python模块中正确使用相对或绝对导入?

我有这个文件布局,如下例所示:
(在这里下载: http://www.mediafire.com/?oug42nzvxrvoms4
http://www.python.org/dev/peps/pep-0328/#guido-s-decision

I have this file layout, as shown in this example: (download here: http://www.mediafire.com/?oug42nzvxrvoms4) http://www.python.org/dev/peps/pep-0328/#guido-s-decision

moduleX包含:

moduleX contains:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

这就是:

C:\package\subpackage1>python moduleX.py
Traceback (most recent call last):
  File "moduleX.py", line 1, in <module>
    from .moduleY import spam
ValueError: Attempted relative import in non-package

我有python 2.7.2。我有

I have python 2.7.2. I have

__init__.py

每个目录中的文件。
为什么这段代码不起作用?

files in every directory. Why does this code not work?

推荐答案

来自文档:

  • http://www.python.org/dev/peps/pep-0328/#guido-s-decision

你可以看到:


相对导入使用模块的 name 属性来确定模块在包层次结构中的位置。如果模块的名称不包含任何包信息(例如,它设置为' main '),则解析相对导入,就像模块是顶级模块一样,无论模块实际位于何处在文件系统上。

Relative imports use a module's name attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to 'main') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system.

通过将其作为 python moduleX.py 运行,你正在做上面的事情。相反,试试这个:

By running it as python moduleX.py, you are doing exactly the above. Instead, try this:

python -m package.subpackage1.moduleX

这将导入moduleX并将顶层放在包中。从层次结构的顶部运行:

This will import moduleX and put the top level at package. Run from the top of the hierarchy:

package/
    __init__.py
    subpackage1/
        __init__.py
        moduleX.py
        moduleY.py
    subpackage2/
        __init__.py
        moduleZ.py
    moduleA.py

ie在你的情况下从 c:\ 直接:

i.e. in your case from c:\ directly:

c:\>python -m package.subpackage1.moduleX

注意一件事 - <$ c中的导入$ c> moduleX.py 是这些:

Note one thing - the imports in moduleX.py are these:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

倒数第二个:

from ...package import bar

需要根文件夹(在你的情况下为 c:\ )作为一个包(即有 __ init __。py ) 。此外,它需要在 package \ _init __。py 中定义的bar变量,这是当前不存在的(所以把 bar ='bar!'有测试)。它还要求你升级一级 - 所以你必须将文件夹放在另一个文件夹中(所以你最终得到 c:\ toppackage\package )并运行 c:\ python -m toppackage.package.subpackage1.moduleX

requires the root folder (c:\ in your case) to be a package (i.e. have __init__.py). Also, it requires bar variable defined in package\__init__.py, which is currently not there (so put bar = 'bar!' there for test). It also requires you to be one level up - so you have to put the package folder in another folder (so you end up with c:\toppackage\package) and run c:\python -m toppackage.package.subpackage1.moduleX.

对于这一行:

from ...sys import path

在上面的PEP 328链接中有一个注释:

there's a note in the above PEP 328 link:


请注意,虽然最后一个案例是合法的,但肯定不鼓励(疯狂是Guido使用的词)。

Note that while that last case is legal, it is certainly discouraged ("insane" was the word Guido used).

另见其他可能有帮助的SOQ:

See also other SOqs about this that might help:

  • How to do relative imports in Python?
  • Can anyone explain python's relative imports?
  • How to accomplish relative import in python
  • python relative import weirdness
  • Relative imports in Python

希望这有帮助。

这篇关于python相对导入示例代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆