擅长:python、mysql、java
<p>可以使用包的<a href="http://docs.python.org/library/functions.html#__import__" rel="nofollow">^{<cd1>}</a>函数和<a href="http://docs.python.org/tutorial/modules.html#packages-in-multiple-directories" rel="nofollow">^{<cd2>}</a>属性的组合。</p>
<p>例如,假设您希望从包中的其他位置导入<code><whatever>.level_one_a.level_two.hello_world</code>。你可以这样做:</p>
<pre><code>import os
_temp = __import__(__path__[0].split(os.sep)[0] + ".level_one_a.level_two.hello_world")
my_hello_world = _temp.level_one_a.level_two.hello_world
</code></pre>
<p>此代码独立于顶级包的名称,可以在包中的任何位置使用。也很难看。</p>