python config_error ai_generated true

ERROR collecting doctests File "...", line 1, in <module> ModuleNotFoundError: No module named 'myapp'

ID: python/pytest-doctest-missing-module

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2025-05-30First Seen

Version Compatibility

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

Root Cause

pytest --doctest-modules discovers .py files under the project tree, including examples/ or docs/ that import the package, but the package is not installed or on sys.path.

generic

中文

pytest --doctest-modules 发现项目树下的 .py 文件(包括 examples/ 或 docs/),这些文件导入包但包未安装或不在 sys.path 上。

Workarounds

  1. 95% success
    pip install -e .
    pytest --doctest-modules src/myapp -q
  2. 90% success
    # pyproject.toml
    [tool.pytest.ini_options]
    testpaths = ['src/myapp', 'tests']
    pythonpath = ['src']
    addopts = '--doctest-modules'

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Skips real doctests in examples; other directories still fail if the package is not importable.

  2. 70% fail

    Boilerplate in every file; breaks when run from a different cwd.