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

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

## 根因

pytest 在项目根目录不在 sys.path 上的目录中运行。这通常发生在测试位于子目录且包未安装，或使用 src 布局但未正确配置时。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Create conftest.py in the project root (even empty). pytest adds the rootdir to sys.path when it finds conftest.py.
   ```
2. **** (98% 成功率)
   ```
   Run: pip install -e .
Ensure setup.py or pyproject.toml is present with proper package discovery.
   ```
3. **** (95% 成功率)
   ```
   Run: python -m pytest tests/
This adds the current directory to sys.path.
   ```

## 无效尝试

- **** — The current working directory may not be the project root, and this is fragile. (70% 失败率)
- **** — This makes the parent directory not importable and often breaks relative imports. (80% 失败率)
