# ModuleNotFoundError: 没有名为 'myapp' 的模块
# 使用 src/ 布局从仓库根运行 pytest 时

- **ID:** `python/pytest-import-mismatch-src-layout`
- **领域:** python
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

src/ 布局要求包已安装或在 sys.path 上；未执行 `pip install -e .` 时从仓库根运行 pytest 无法访问 src/myapp。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (98% 成功率)
   ```
   pip install -e .
# or with build backend
pip install -e ".[test]"

# then
pytest
   ```
2. **** (92% 成功率)
   ```
   # pyproject.toml
[tool.pytest.ini_options]
pythonpath = ["src"]
testpaths = ["tests"]
   ```

## 无效尝试

- **** — Duplicated boilerplate and still fails for subprocess-based tests or coverage tools that re-import. (80% 失败率)
- **** — Abandons src/ layout benefits (isolation, editable install correctness) and may break packaging. (70% 失败率)
- **** — Not reproducible in CI; each developer must remember the env var. (85% 失败率)
