Python Py.test 没有名为 * 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20985157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:42:48  来源:igfitidea点击:

Py.test No module named *

pythonpytest

提问by zsljulius

I have a folder structure like this

我有这样的文件夹结构

App
--App
  --app.py       
--Docs
--Tests
  --test_app.py

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?

在 my 中test_app.py file,我有一行导入我的应用程序模块。当我在根文件夹上运行 py.test 时,我收到关于没有名为 app 的模块的错误。我应该如何配置它?

采纳答案by Dun Peal

So you are running py.testfrom /App. Are you sure /App/Appis in your $PYTHONPATH?

所以你py.test/App. 你确定/App/App在你的$PYTHONPATH

If it's not, code that tries to import appwill fail with such a message.

如果不是,则尝试执行此操作的代码import app将失败并显示此类消息。

EDIT0:including the info from my comment below, for completeness.

EDIT0:为了完整性,包括我下面评论中的信息。

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/Appa package by putting __init__.pyinside it, and change your import to qualify app as from App import app.

导入 app 的尝试只有在内部执行时才会成功/App/App,这里不是这种情况。您可能希望/App/App通过放入其中来制作一个包__init__.py,然后更改您的导入以将应用程序限定为from App import app.

EDIT1:by request, adding further explanation from my second comment below.

EDIT1:根据要求,从我下面的第二条评论中添加进一步的解释。

By putting __init__.pyinside /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 appif /Appis in the $PYTHONPATH. Your current working directory gets automatically added to $PYTHONPATH, so when you run a script from /App, the import will work.

通过放入__init__.pyinside /App/App,该目录成为一个包。这意味着您可以从中导入,只要它 - 目录 - 在$PYTHONPATH. 即您可以从App import appif/App中执行$PYTHONPATH。您当前的工作目录会自动添加到$PYTHONPATH,因此当您从 运行脚本时/App,导入将起作用。

回答by berezovskyi

I already had an __init__.pyfile in the /App/Appdirectory and wanted to run tests from the project root without any path-mangling magic:

我已经__init__.py/App/App目录中有一个文件,并希望从项目根目录运行测试,而无需任何路径修改魔法:

 python -m pytest tests

The output immediately looks like this:

输出立即如下所示:

?  python -m pytest tests
====================================== test session starts ======================================
platform linux -- Python 3.5.1, pytest-2.9.0, py-1.4.31, pluggy-0.3.1
rootdir: /home/andrew/code/app, inifile: 
plugins: teamcity-messages-1.17
collected 46 items 

... lines omitted ...

============================= 44 passed, 2 skipped in 1.61 seconds ==============================

回答by jamesc

Working with Python 3 and getting the same error on a similar project layout, I solved it by adding an __init__file to my testsmodule.

使用 Python 3 并在类似的项目布局上遇到相同的错误,我通过向__init__我的tests模块添加一个文件来解决它。

$ touch tests/__init__.py

I'm not great at packaging and importing, but I think that this helps pytestwork out where the target Appmodule is located.

我不擅长打包和导入,但我认为这有助于pytest确定目标App模块的位置。

回答by Jayant

I also got same error while running test cases for my app located as below

在为我的应用程序运行测试用例时,我也遇到了同样的错误,如下所示

myproject
  --app1
    --__init.py__
    --test.py
  --app2
    --__init.py__
    --test.py
  --__init.py__

I deleted my myproject's__init.py__file and boom, test cases are running now.

我删除了我的项目__init.py__文件和繁荣,现在正在运行测试用例。