sphinx autodoc "ImportError: cannot import name" documented module
Fixes Sphinx autodoc builds that fail with ImportError when the documented module cannot be imported. Use when autodoc directives trigger cannot-import-name errors. Not for toctree warnings or extension load failures.
TL;DR
Make the module importable from the Sphinx process: add the package source directory to sys.path in conf.py and install the package's dependencies. Autodoc imports your code for real, so anything that breaks a normal import breaks the docs build.
Error
WARNING: autodoc: failed to import module 'mylib.client'; the following exception was raised:
ImportError: cannot import name 'Client' from 'mylib.client'Steps
- Reproduce the import outside Sphinx with
python -c "import mylib.client"from the docs directory. Expected: you see the same ImportError, confirming it is not a Sphinx problem. - Open conf.py and add the source root to the path, e.g.
sys.path.insert(0, os.path.abspath('../src')), using the layout your project actually uses. Expected: the path entry points at the directory containing your top-level package. - Install the package and its runtime dependencies into the environment that runs the build:
pip install -e .orpip install -r requirements.txt. Expected:pip show mylibreports the package as installed. - Re-run the import from step 1, then rebuild with
sphinx-build -b html docs docs/_build. Expected: the import succeeds and autodoc warnings are gone.
When to use
- Autodoc reports ImportError or ModuleNotFoundError for your own package
- Docs build works locally but fails in CI or on Read the Docs
- The package imports fine in your shell but not during the docs build
When not to use
- The module imports but a documented attribute is missing (check
__all__and spelling instead) - The failure is a missing third-party dependency only needed at import time (use autodocmockimports for that)
- The error is in an extension, not in autodoc
Tool compatibility
Sphinx 5.x through 8.x with sphinx.ext.autodoc. Path handling identical on all platforms; on Windows use raw strings or forward slashes in conf.py paths.
Variant phrasings
autodoc ModuleNotFoundError for a namespace package
Same fix, but verify the namespace package has no conflicting installed copy shadowing your source tree: check python -c "import mylib; print(mylib.__file__)".
autodoc fails only on Read the Docs
The RTD environment installs from your requirements file. If the dependency is missing there, add it to the docs requirements file, not just your local environment.
Why it happens
Autodoc executes import on every documented module inside the Sphinx process. The Sphinx process does not automatically know where your source lives, and CI builders start from a clean environment. Any gap between "imports in my terminal" and "imports in the build environment" surfaces as an autodoc ImportError.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_yxFvF7puklOXz9n-R0X6JA