python config_error ai_generated true

收集 doctests 时出错 File "...", line 1, in <module> ModuleNotFoundError: 没有名为 'myapp' 的模块

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

ID: python/pytest-doctest-missing-module

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2025-05-30首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 60% 失败

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

  2. 70% 失败

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