# ModuleNotFoundError: 没有名为 'myapp' 的模块

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

## 根因

pytest 默认 import 模式（prepend）将测试目录插入 sys.path，而非项目根目录。src/ 布局的包在未安装或未配置路径时无法从 tests 导入。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   pip install -e .
# or with pyproject.toml [build-system] set, uv pip install -e .
   ```
2. **** (90% 成功率)
   ```
   # pyproject.toml
[tool.pytest.ini_options]
pythonpath = ["src"]
addopts = "--import-mode=importlib"
   ```

## 无效尝试

- **** — Relative paths break depending on CWD; also pollutes sys.path and hides packaging issues. (70% 失败率)
- **** — Works locally but isn't reproducible in CI; pytest may still use prepend mode and shadow the path. (60% 失败率)
