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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 95% 成功率
    pip install -e .
    pytest --doctest-modules
  2. 90% 成功率
    In pytest.ini: `[pytest]
    pythonpath = .`

无效尝试

常见但无效的做法:

  1. 60% 失败

    May work locally but fails in CI where the working directory differs.

  2. 80% 失败

    Loses doctest coverage; not a fix.