擅长:python、mysql、java
<p>您可以使用<code>__file__</code>全局属性来获取当前模块的源路径。在</p>
<p>从2.6文件中:</p>
<blockquote>
<p><code>__file__</code> is the pathname of the file from which the module was loaded, if
it was loaded from a file. The
<code>__file__</code> attribute is not present for C modules that are statically linked
into the interpreter; for extension
modules loaded dynamically from a
shared library, it is the pathname of
the shared library file.</p>
</blockquote>
<p>编辑:</p>
<p>我假设inspect.getsourcefile文件()在检查<code>__main__</code>模块时总是抛出TypeError。只有从交互式解释器运行时才会出现这种情况。我站在正确的立场上。德普。在</p>