我的树看起来像
parent/
|--__init__.py
\--a.py
而__init__.py的内容是
import parent.a as _a
a = 'some string'
当我在顶层打开 Python 并import parent.a 时,我会得到字符串而不是模块。例如 import parent.a as the_a;类型(the_a)== str。
所以我认为可能 import 正在从 parent 命名空间导入名称,现在它已被覆盖。所以我想我可以 import parent._a as a_module。但这不起作用,因为没有“没有名为 _a 的模块”。
这很令人困惑。函数可以覆盖具有相同名称的模块,但模块不能采用新名称并“重新导出”。
有什么我不知道的解释吗?或者这是记录在案的功能?
更令人困惑的是,如果我删除 __init__.py 中的 import 语句,一切又恢复正常(import parent.a; type(parent. a) 是模块)。但为什么这不同呢? parent 命名空间中的 a 名称仍然是一个字符串。
(我在 Python 3.5.3 和 2.7.13 上运行,结果相同)
请您参考如下方法:
在 import 语句中,模块引用从不 使用属性查找。声明
import parent.a # as ...
和
from parent.a import ... # as ...
在尝试进一步从磁盘加载模块之前,总是会在 sys.modules 命名空间中寻找 parent.a。
但是,对于 from ... import name 语句,Python 会在查找子模块之前查看已解析模块的属性以找到 name。
模块全局变量和模块对象的属性是一回事。在导入时,Python 将子模块作为属性(因此是全局变量)添加到父模块,但您可以自由地覆盖这些属性,就像您在代码中所做的那样。但是,当您随后使用带有 parent.a 模块路径的导入时,属性不会发挥作用。
来自Submodules section of the Python import system reference documentation :
When a submodule is loaded using any mechanism [...] a binding is placed in the parent module’s namespace to the submodule object. For example, if package
spamhas a submodulefoo, after importingspam.foo,spamwill have an attributefoowhich is bound to the submodule.
您的import parent.a as _a 语句将两个 名称添加到parent 命名空间;首先添加 a 指向 parent.a 子模块,然后 _a 也被设置,指向同一个对象。
您的下一行将名称 a 替换为到 'some string' 对象的绑定(bind)。
Searching section of the same详细介绍了 Python 如何在您导入时查找模块:
To begin the search, Python needs the fully qualified name of the module [...] being imported.
[...]
This name will be used in various phases of the import search, and it may be the dotted path to a submodule, e.g.
foo.bar.baz. In this case, Python first tries to importfoo, thenfoo.bar, and finallyfoo.bar.baz. If any of the intermediate imports fail, aModuleNotFoundErroris raised.
然后进一步
The first place checked during import search is
sys.modules. This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths. So iffoo.bar.bazwas previously imported,sys.moduleswill contain entries forfoo,foo.bar, andfoo.bar.baz. Each key will have as its value the corresponding module object.During import, the module name is looked up in
sys.modulesand if present, the associated value is the module satisfying the import, and the process completes. [...] If the module name is missing, Python will continue searching for the module.
因此,当尝试导入 parent.a 时,重要的是 sys.modules['parent.a'] 存在。 sys.modules['parent'].a 未被查询。
只有 from module import ... 会查看属性。来自import statement documentation :
The
fromform uses a slightly more complex process:
- find the module specified in the from clause, loading and initializing it if necessary;
- for each of the identifiers specified in the import clauses:
- check if the imported module has an attribute by that name
- if not, attempt to import a submodule with that name and then check the imported module again for that attribute
- [...]
所以 from parent import _a 会起作用,就像 from parent import a 一样,你会得到 parent.a 子模块和'some string' 对象。
请注意 sys.modules 是可写的,如果您必须让 import parent._a 工作,您可以随时更改 sys.modules 直接:
sys.modules['parent._a'] = sys.modules['parent.a'] # make parent._a an alias for parent.a
import parent._a # works now
