擅长:python、mysql、java
<p>Python不会隐式导入整个包层次结构。必须明确使用<code>__init__.py</code>文件在包的哪个级别导入什么。在</p>
<p>当您在<code>__init__.py</code>中设置<code>__all__ = ['knights']</code>时,它只适用于该模块的<em>import all</em>语句,例如:</p>
<pre><code>>>> import holygrail
>>> holygrail.knights.say()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'knights'
>>> from holygrail import *
>>> knights.say()
Ni!
</code></pre>
<p>它还可以作为<em>import all</em>上的过滤器,只从模块中导入指定的内容。在</p>
<p>要使<code>knights</code>自动导入到<code>import holygrail</code>上,必须将<code>import knights</code>或{<cd7>}(<a href="https://docs.python.org/2/tutorial/modules.html#intra-package-references" rel="nofollow">intra-package or relative import</a>)放入<code>__init__.py</code>。您必须对每个模块显式地执行相同的操作。在</p>