python
module_error
ai_generated
partial
收集 mypkg/mod.py 时出错 E ImportError: 无法从 'mypkg.mod' 导入名称 'helper'(位置未知)
ERROR collecting mypkg/mod.py E ImportError: cannot import name 'helper' from 'mypkg.mod' (unknown location)
ID: python/pytest-doctest-import-failure
80%修复率
80%置信度
0证据数
2024-09-25首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
pytest --doctest-modules 会尝试导入每个模块,包括有副作用或循环导入的模块,导致收集阶段导入错误。
English
pytest --doctest-modules tries to import every module, including ones with side effects or circular imports, causing collection-time import errors.
解决方案
-
85% 成功率
Fix the circular import by moving the import inside the function, or use `--doctest-modules` with `--import-mode=importlib`.
-
80% 成功率
Exclude problematic modules explicitly: [tool.pytest.ini_options] addopts = "--doctest-modules --ignore=mypkg/cli.py"
无效尝试
常见但无效的做法:
-
55% 失败
Skips doctests for that module entirely, losing coverage.
-
60% 失败
The module still can't be imported due to the underlying circular import or missing dependency.