python
module_error
ai_generated
true
Failed: [doctest] mymodule.func 意外异常: ModuleNotFoundError: 没有名为 'mymodule' 的模块
Failed: [doctest] mymodule.func UNEXPECTED EXCEPTION: ModuleNotFoundError: No module named 'mymodule'
ID: python/pytest-doctest-import-error
80%修复率
85%置信度
0证据数
2024-10-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
pytest-doctest 尝试从 doctest 文件导入模块,但模块不在 sys.path 上,因为测试从不同目录运行。
English
pytest-doctest tries to import the module from the doctest file, but the module is not on sys.path because tests run from a different directory.
解决方案
-
95% 成功率
pip install -e . pytest --doctest-modules
-
90% 成功率
In pytest.ini: `[pytest] pythonpath = .`
无效尝试
常见但无效的做法:
-
60% 失败
May work locally but fails in CI where the working directory differs.
-
80% 失败
Loses doctest coverage; not a fix.