python module_error ai_generated true

Failed: [doctest] mymodule.func UNEXPECTED EXCEPTION: ModuleNotFoundError: No module named 'mymodule'

ID: python/pytest-doctest-import-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-10-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

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

中文

pytest-doctest 尝试从 doctest 文件导入模块,但模块不在 sys.path 上,因为测试从不同目录运行。

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 60% fail

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

  2. 80% fail

    Loses doctest coverage; not a fix.