Sphinx AutoAPI
Sphinx AutoAPI is an extension for Sphinx that automatically generates API documentation from your Python code, similar to `sphinx.ext.autodoc` but without needing to explicitly list every module, class, or function. It parses your source code and creates reStructuredText files. The current version is 3.8.0, and it typically releases new minor versions every few months.
Warnings
- breaking AutoAPI frequently drops support for older Python versions. Ensure your environment uses Python 3.10 or newer. Version 3.6.0 dropped Python 3.8, and 3.7.0 dropped Python 3.9.
- gotcha The `autoapi_dirs` configuration in `conf.py` must correctly point to your Python source code. Relative paths should be correctly resolved, often using `os.path.abspath` and `os.path.join`.
- gotcha Prior to v3.7.0, AutoAPI would silently fail to document objects specified in directives (e.g., `.. py:module::`) that did not exist in the source code. From v3.7.0, an `autoapi.import` warning is issued.
- breaking Compatibility with the `astroid` library, used for parsing, can be sensitive. `v3.6.1` fixed a breaking change in `astroid`'s `Import` mechanism, indicating that direct `astroid` upgrades might cause issues with older `sphinx-autoapi` versions.
Install
-
pip install sphinx-autoapi
Imports
- autoapi.extension
extensions = ['autoapi.extension']
Quickstart
import os
# In conf.py
project = 'My Project'
copyright = '2024, Author'
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'autoapi.extension',
]
html_theme = 'sphinx_rtd_theme'
# Point AutoAPI to your source code directory(ies)
# Adjust '../src' to your actual project source path relative to conf.py
autoapi_dirs = [os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src'))]
# Optional: Generate a main index file for AutoAPI
# Then, link to it in your main index.rst via:
# .. toctree::
# :maxdepth: 2
# :caption: API
# _autoapi/index