从项目的根部执行pytest时,modulenotfounderror:无模块名称' main'
问题:当我从cmd py -m pytest py_src/
中从项目的根中执行pytest。我会收到以下错误:modulenotfoundError:no模块名为“ main”
。但是,如果我将cd cd到py_src/中,然后运行py -m pytest 一切都相应。我对Pytest仍然相对较新,将不胜感激的是将我指向正确的方向。
目标:我要实现的目标是从项目的根部创建一个.yml文件,该文件将在其他目录中运行Pytest和其他测试。
当前状态:这是我的文件结构的样子。
root
|
|---py_src
| |
| |---main.py
| |
| |---tests
| | |
| | |---test_x.py
在我的tests.py文件中,我有
from main import app
PROBLEM: When I execute pytest from the root of the project in the cmd py -m pytest py_src/
. I get the following error: ModuleNotFoundError: No module named 'main'
. However if I cd into py_src/ and then run py -m pytest
everything behaves accordingly. I am still relatively new with pytest, anything would be appreciated to direct me in the right direction.
GOAL: What I am trying to achieve is to create a .yml file from the root of the project that will run the pytest and other tests in other directories.
CURRENT STATE: here is what my file structure looks like.
root
|
|---py_src
| |
| |---main.py
| |
| |---tests
| | |
| | |---test_x.py
in my tests.py file, I have
from main import app
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过在每个目录中添加名称
__ INIT __。py
来制作目录模块。现在,将您在
test_x.py
中更改您的导入到从模块中导入,例如从py_src.main导入app
这将允许您从root或source目录运行pytest
make the directories modules by adding a blank file by the name
__init__.py
in each directory.Now change your import in
test_x.py
to import from module likefrom py_src.main import app
This will allow you to run the pytest from either root or from the source directory