# 收集 mypkg/mod.py 时出错
E   ImportError: 无法从 'mypkg.mod' 导入名称 'helper'（位置未知）

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

## 根因

pytest --doctest-modules 会尝试导入每个模块，包括有副作用或循环导入的模块，导致收集阶段导入错误。

## 版本兼容性

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

## 解决方案

1. **** (85% 成功率)
   ```
   Fix the circular import by moving the import inside the function, or use `--doctest-modules` with `--import-mode=importlib`.
   ```
2. **** (80% 成功率)
   ```
   Exclude problematic modules explicitly:
[tool.pytest.ini_options]
addopts = "--doctest-modules --ignore=mypkg/cli.py"
   ```

## 无效尝试

- **** — Skips doctests for that module entirely, losing coverage. (55% 失败率)
- **** — The module still can't be imported due to the underlying circular import or missing dependency. (60% 失败率)
