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__.pyhas to contain a line similar tofrom . import package(the namepackagehas to be defined inside thisbasepackage/__init__.pyfile)basepackage/package/__init__.pyhas to contain a line similar tofrom . import subpackagebasepackage/package/subpackage/__init__.pyhas 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 中导入深度嵌套的模块
基础教程推荐
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
