Importing deeply nested modules in Python(在 Python 中导入深度嵌套的模块)
问题描述
考虑 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 tofrom . import package
(the namepackage
has to be defined inside thisbasepackage/__init__.py
file)basepackage/package/__init__.py
has to contain a line similar tofrom . import subpackage
basepackage/package/subpackage/__init__.py
has to contain a line similar tofrom . 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 中导入深度嵌套的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中导入深度嵌套的模块


基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01