带Flask的pytest找不到modu

2024-06-23 19:02:00 发布

您现在位置:Python中文网/ 问答频道 /正文

按照本教程https://flask.palletsprojects.com/en/1.1.x/#user-s-guide我正在学习烧瓶但是,当我试图运行pytest时,我得到了这个错误:

(FlaskVenv) MacBook-Pro-de-Hugo:FlaskProject hugovillalobos$ pytest
ImportError while loading conftest '/Users/hugovillalobos/Documents/Code/FlaskProject/tests/conftest.py'.
tests/conftest.py:4: in <module>
    from Flaskr import create_app
E   ModuleNotFoundError: No module named 'Flaskr'

这是引发错误的模块(conftest.py):

^{pr2}$

这是教程中所示的模块代码(唯一的区别是我的模块大写):

enter image description here

我认为我的文件位置可能有问题,但我正确地映射了教程项目布局:

enter image description here

我不知道我错过了什么。在


Tags: 模块pyhttpscomflaskpytest错误tests
1条回答
网友
1楼 · 发布于 2024-06-23 19:02:00

您可能需要在python中将pytest作为一个模块运行(它将当前目录添加到搜索路径). 我记得有过类似的情况,这就解决了我的问题:

python -m pytest -vv

来源:https://docs.pytest.org/en/latest/usage.html#calling-pytest-through-python-m-pytest

您可以从命令行通过Python解释器调用测试:

python-m pytest[…] 这几乎等同于直接调用命令行脚本pytest[…],只是通过python调用也会将当前目录添加到搜索路径. 在

相关问题 更多 >

    热门问题