考虑 Python 3.6 中的以下情况:
Consider the following case in Python 3.6:
basepackage
|---__init__.py
|---package
|---__init__.py
|---subpackage
|---__init__.py
|---module.py
重要细节:在 basepackage.package.__init__.py
里面有:
from basepackage.package.subpackage.module import AClass as AliasedClass
现在,让我们说在 basepackage.package.subpackage.module.py
中我们要使用:
Now, let's say inside basepackage.package.subpackage.module.py
we want to use:
将 basepackage.package.subpackage.module 导入为 aliased_module
[1]
结果是:
AttributeError: module 'basepackage' has no attribute 'package'
在罪魁祸首语句之后列出堆栈跟踪(按以下顺序):
with a stack trace listing following culprit statements (in the below order):
from basepackage.package.subpackage.module import AClass as AliasedClass
import basepackage.package.subpackage.module as aliased_module
但如果你想使用 [1]
而不是:
But if instead of [1]
one'd like to use:
from basepackage.package.subpackage import module as aliased_module
[2]
然后一切正常.
[1]
与 [2]
有何不同,前者会导致错误,而后者不会?
How is [1]
so much different than [2]
that the former results in an error and the latter not?
要使第一个选项(import basepackage.package.subpackage.module as aliased_module
)起作用,必须满足以下条件:
For the first option (import basepackage.package.subpackage.module as aliased_module
) to work, these conditions have to be met:
basepackage/__init__.py
必须包含类似于 from 的行.import package
(名称package
必须在这个basepackage/__init__.py
文件中定义)basepackage/package/__init__.py
必须包含类似于 from 的行.导入子包
basepackage/package/subpackage/__init__.py
必须包含类似于 from 的行.导入模块
basepackage/__init__.py
has to contain a line similar to from . import package
(the name package
has to be defined inside this basepackage/__init__.py
file)basepackage/package/__init__.py
has to contain a line similar to from . import subpackage
basepackage/package/subpackage/__init__.py
has to contain a line similar to from . import module
注意:__init__.py
文件中的 import 语句也可以是绝对路径而不是相对路径.
Note: the import statements inside the __init__.py
files can also be absolute instead of relative paths.
对于第二个选项(from basepackage.package.subpackage import module as aliased_module
),如果每个级别都有空的 __init__.py
文件就足够了,如只要这些 __init__.py
文件存在.
For the second option (from basepackage.package.subpackage import module as aliased_module
), it is enough if there are empty __init__.py
files at each level, as long as these __init__.py
files exist.
这篇关于在 Python 中导入深度嵌套的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!