The submodules often need to refer to each other. For example, the
surround module might use the echo module. In fact,
such references
are so common that the import statement first looks in the
containing package before looking in the standard module search path.
Thus, the surround module can simply use import echo
or
from echo import echofilter
. If the imported module is not
found in the current package (the package of which the current module
is a submodule), the import statement looks for a top-level
module with the given name.
子模块之间经常需要互相引用。例如,surround 模块可能会引用
echo 模块。事实上,这样的引用如此普遍,以致于
import
语句会先搜索包内部,然后才是标准模块搜索路径。因此 surround 模块
可以简单的调用 import echo
或者 from echo import
echofilter
。如果没有在当前的包中发现要导入的模块,
import 语句会依据指定名寻找一个顶级模块。
When packages are structured into subpackages (as with the
Sound package in the example), there's no shortcut to refer
to submodules of sibling packages - the full name of the subpackage
must be used. For example, if the module
Sound.Filters.vocoder needs to use the echo module
in the Sound.Effects package, it can use from
Sound.Effects import echo
.
如果包中使用了子包结构(就像示例中的 Sound
包),不存在什么从邻近的包中引用子模块的便捷方法--必须使用子包的全名。例如,如果
Sound.Filters.vocoder 包需要使用 Sound.Effects
包中的 echosa 模块,它可以使用 from Sound.Effects
import echo
。