擅长:python、mysql、java
<p>为了详细说明Ferdinand Beyer的<a href="https://stackoverflow.com/questions/6576351/how-to-import-module-from-current-non-default-directory/6576445#6576445">answer</a>,<code>sys.path</code>是默认模块导入程序检查的文件位置列表。有些,尽管不是所有python安装都会将当前目录或<code>__main__</code>模块的目录添加到路径中。要确保与给定模块相关的路径在该模块中可导入,请执行以下操作:</p>
<pre><code>import os.path, sys
sys.path.append(os.path.dirname(__file__))
</code></pre>
<p>但这样的东西不应该成为“生产”产品。相反,使用类似<code>distutils</code>的方法将模块的包安装到python站点包目录中。</p>