<p>来自<code>__import__</code>上的python文档:</p>
<blockquote>
<pre><code>__import__( name[, globals[, locals[, fromlist[, level]]]])
</code></pre>
<p>...</p>
<p>When the name variable is of the form
package.module, normally, the
top-level package (the name up till
the first dot) is returned, not the
module named by name. However, when a
non-empty fromlist argument is given,
the module named by name is returned.
This is done for compatibility with
the bytecode generated for the
different kinds of import statement;
when using "import spam.ham.eggs", the
top-level package spam must be placed
in the importing namespace, but when
using "from spam.ham import eggs", the
spam.ham subpackage must be used to
find the eggs variable. As a
workaround for this behavior, use
getattr() to extract the desired
components. For example, you could
define the following helper:</p>
<pre><code>def my_import(name):
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
</code></pre>
</blockquote>
<p><strong>意译如下:</strong></p>
<p>当您请求<code>somepackage.somemodule</code>时,<code>__import__</code>返回<code>somepackage.__init__.py</code>,通常是空的。</p>
<p>如果您提供<code>fromlist</code>(您想要的<code>somemodule</code>内的变量名列表,它将返回<code>somemodule</code>(实际上没有返回)</p>
<p>你也可以像我一样,使用他们建议的功能。</p>
<p>注:我问这个问题完全是想自己回答。我的代码中有一个很大的错误,由于被误诊了,我花了很长时间才弄明白,所以我想我应该帮助so社区解决这个问题,把我遇到的问题贴在这里。</p>