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

- **ID:** `python/pytest-collection-error-modulenotfounderror`
- **领域:** python
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pytest 找不到指定的测试模块，因为当前工作目录不在 sys.path 中，或者模块路径不正确。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Run pytest from the project root directory or use the correct path: `pytest tests/`
   ```
2. **** (85% 成功率)
   ```
   Set PYTHONPATH environment variable: `PYTHONPATH=. pytest`
   ```

## 无效尝试

- **** — Modifying sys.path in conftest.py may not apply early enough during collection, and can cause inconsistent behavior across test runs. (60% 失败率)
- **** — The --pyargs flag requires the module to be installed as a package; otherwise it fails silently or raises the same error. (70% 失败率)
