Py.test 没有名为 * 的模块 [英] Py.test No module named *
问题描述
我有这样的文件夹结构
App
--App
--app.py
--Docs
--Tests
--test_app.py
在我的 test_app.py 文件
中,我有一行用于导入我的应用程序模块.当我在根文件夹上运行 py.test 时,我收到关于没有名为 app 的模块的错误.我应该如何配置它?
In my test_app.py file
, I have a line to import my app module. When I run py.test on the root folder, I get this error about no module named app. How should I configure this?
推荐答案
所以你从 /App
运行 py.test
.您确定 /App/App
在您的 $PYTHONPATH
中吗?
So you are running py.test
from /App
. Are you sure /App/App
is in your $PYTHONPATH
?
如果不是,尝试导入应用
的代码将失败并显示这样的消息.
If it's not, code that tries to import app
will fail with such a message.
为了完整起见,包括我在下面评论中的信息.
including the info from my comment below, for completeness.
尝试导入应用程序只有在 /App/App
中执行时才会成功,这里不是这种情况.您可能希望通过将 __init__.py
放入其中来制作 /App/App
一个包,并更改您的导入以将应用程序限定为 from App import app代码>.
An attempt to import app will only succeed if it was executed inside /App/App
, which is not the case here. You probably want to make /App/App
a package by putting __init__.py
inside it, and change your import to qualify app as from App import app
.
根据要求,从我下面的第二条评论中添加进一步的解释.
by request, adding further explanation from my second comment below.
通过将 __init__.py
放在 /App/App
中,该目录成为一个包.这意味着您可以从中导入,只要它 - 目录 - 在 $PYTHONPATH
中可见.IE.如果 /App
在 $PYTHONPATH
中,您可以从 App import app
执行.您当前的工作目录会自动添加到 $PYTHONPATH
,因此当您从 /App
运行脚本时,导入将起作用.
By putting __init__.py
inside /App/App
, that directory becomes a package. Which means you can import from it, as long as it - the directory - is visible in the $PYTHONPATH
. I.e. you can do from App import app
if /App
is in the $PYTHONPATH
. Your current working directory gets automatically added to $PYTHONPATH
, so when you run a script from /App
, the import will work.
这篇关于Py.test 没有名为 * 的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!