# 收集 docs/usage.rst 时出错
E   ModuleNotFoundError: 没有名为 'mypkg' 的模块
# 运行 pytest --doctest-glob='*.rst' 时

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

## 根因

在 .rst 文件中收集 doctest 时，docs 目录不在 sys.path 上，因此代码块中的导入失败，即使包已以可编辑模式安装。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   Set pythonpath in pytest.ini so the package is importable from any cwd:

[pytest]
pythonpath = . src
addopts = --doctest-glob='*.rst'
   ```
2. **** (90% 成功率)
   ```
   Or install editable before running doctests:

pip install -e . && pytest --doctest-glob='*.rst' docs/
   ```

## 无效尝试

- **** — Doctest code blocks are not executable module-level statements; the insert runs but relative to the wrong cwd. (85% 失败率)
- **** — Installs a copy that may not reflect the working tree, causing stale doctest results. (75% 失败率)
