python
module_error
ai_generated
true
ModuleNotFoundError: 没有名为 'myapp' 的模块 # 使用 src/ 布局从仓库根运行 pytest 时
ModuleNotFoundError: No module named 'myapp' # when running pytest from repo root with src/ layout
ID: python/pytest-import-mismatch-src-layout
80%修复率
90%置信度
0证据数
2025-06-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
src/ 布局要求包已安装或在 sys.path 上;未执行 `pip install -e .` 时从仓库根运行 pytest 无法访问 src/myapp。
English
src/ layout requires the package to be installed or on sys.path; running pytest from the repo root without `pip install -e .` leaves src/myapp unreachable.
解决方案
-
98% 成功率
pip install -e . # or with build backend pip install -e ".[test]" # then pytest
-
92% 成功率
# pyproject.toml [tool.pytest.ini_options] pythonpath = ["src"] testpaths = ["tests"]
无效尝试
常见但无效的做法:
-
80% 失败
Duplicated boilerplate and still fails for subprocess-based tests or coverage tools that re-import.
-
70% 失败
Abandons src/ layout benefits (isolation, editable install correctness) and may break packaging.
-
85% 失败
Not reproducible in CI; each developer must remember the env var.