python
module_error
ai_generated
partial
ERROR collecting mypkg/mod.py E ImportError: cannot import name 'helper' from 'mypkg.mod' (unknown location)
ID: python/pytest-doctest-import-failure
80%Fix Rate
80%Confidence
0Evidence
2024-09-25First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
Root Cause
pytest --doctest-modules tries to import every module, including ones with side effects or circular imports, causing collection-time import errors.
generic中文
pytest --doctest-modules 会尝试导入每个模块,包括有副作用或循环导入的模块,导致收集阶段导入错误。
Workarounds
-
85% success
Fix the circular import by moving the import inside the function, or use `--doctest-modules` with `--import-mode=importlib`.
-
80% success
Exclude problematic modules explicitly: [tool.pytest.ini_options] addopts = "--doctest-modules --ignore=mypkg/cli.py"
Dead Ends
Common approaches that don't work:
-
55% fail
Skips doctests for that module entirely, losing coverage.
-
60% fail
The module still can't be imported due to the underlying circular import or missing dependency.