Skip to content

Commit 4fbd368

Browse files
authored
DOC: Add autodoc section "Ensuring the code can be imported" (#12426)
This explains the requirements for importablilty (can be found and dependencies can be resolved). It also explains two common approaches how this can be achieved.
1 parent da076d1 commit 4fbd368

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

doc/usage/extensions/autodoc.rst

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
This extension can import the modules you are documenting, and pull in
1313
documentation from docstrings in a semi-automatic way.
1414

15-
.. note::
16-
17-
For Sphinx (actually, the Python interpreter that executes Sphinx) to find
18-
your module, it must be importable. That means that the module or the
19-
package must be in one of the directories on :data:`sys.path` -- adapt your
20-
:data:`sys.path` in the configuration file accordingly.
21-
2215
.. warning::
2316

2417
:mod:`~sphinx.ext.autodoc` **imports** the modules to be documented. If any
@@ -44,6 +37,39 @@ docstrings to correct reStructuredText before :mod:`autodoc` processes them.
4437
.. _NumPy: https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard
4538

4639

40+
Ensuring the code can be imported
41+
---------------------------------
42+
43+
:mod:`~sphinx.ext.autodoc` analyses the code and docstrings by introspection after
44+
importing the modules. For importing to work. you have to make sure that your
45+
modules can be found by sphinx and that dependencies can be resolved (if your
46+
module does ``import foo``, but ``foo`` is not available in the python environment
47+
that Sphinx runs in, your module import will fail).
48+
49+
There are two ways to ensure this:
50+
51+
1. Use an environment that contains your package and Sphinx. This can e.g. be your
52+
local dev environment (with an editable install), or an environment in CI in
53+
which you install Sphinx and your package. The regular installation process
54+
ensures that your package can be found by sphinx and that all dependencies are
55+
available.
56+
57+
2. It is alternatively possible to patch the Sphinx run so that it can operate
58+
directly on the sources; e.g. if you want to be able to do a sphinx build from a
59+
source checkout.
60+
61+
- Patch :data:`sys.path` in your Sphinx :file:`conf.py` to include the folder of
62+
your sources. E.g. if you have a repository structure with :file:`doc/conf.py`
63+
and your package is at :file:`src/mypackage`, then you sould add::
64+
65+
sys.path.insert(0, os.path.abspath('../src'))
66+
67+
to your :file:`conf.py`.
68+
69+
- To cope with missing dependencies, specify the missing modules in
70+
:confval:`autodoc_mock_imports`.
71+
72+
4773
Directives
4874
----------
4975

0 commit comments

Comments
 (0)