擅长:python、mysql、java
<p>在主.py->;<code>import constants</code>是一个隐式相对导入(bad)。它应该改成更常见的<code>from my_project import constants</code>。在</p>
<p>你提到这会让鼻子测试有效。注意:您不需要tests子目录中的<code>__init__.py</code>,事实上,这通常是不可取的。在</p>
<p>现在要修复您的错误,<code>python main.py</code>有一个导入错误,如果您没有将它包含在您的<code>sys.path</code>中,这是正常的。有很多方法可以解决这个问题-</p>
<ul>
<li>从项目根目录执行(即最上面的my\u project one)</li>
<li>设置PYTHONPATH环境变量</li>
<li>使用setuptools/easy-install等正确打包并安装应用程序</li>
</ul>