Setting Up Sphinx and ReadTheDocs for Automated Documentation in Python Projects
This guide explains why Sphinx and ReadTheDocs are a powerful combination for generating and hosting documentation, and provides step‑by‑step instructions—including installing dependencies, initializing the project, configuring conf.py, adding a .readthedocs.yml file, and linking the repository—so that documentation is built automatically on each commit.
In daily open‑source projects or team collaborations, a maintainable, automatically deployed documentation system is often needed.
Why Choose Sphinx and ReadTheDocs
Sphinx is a Python‑based documentation generator originally created for the Python official docs, supporting reStructuredText and Markdown via plugins.
ReadTheDocs is a hosting platform that can automatically pull code from a Git repository, build the docs, and publish them, with webhook support.
The combination is ideal for continuous documentation maintenance, with a mature community and abundant resources.
Basic Configuration Steps
1. Install Sphinx and Related Dependencies
It is recommended to use a virtual environment and install the following requirements:
# docs/requirements.txt
sphinx==5.3.0
sphinx_rtd_theme==1.1.1
# If you need Markdown support, add:
myst_parser==0.18.1Install the dependencies:
pip install -r docs/requirements.txtsphinx-rtd-theme is the default theme used by ReadTheDocs.
myst-parser enables Markdown support.
2. Initialise the Documentation Project Structure
Run the following command at the project root:
sphinx-quickstart docsSeparate the source and build directories. After execution, the docs folder will contain conf.py , index.rst , and other files.
3. Modify conf.py Configuration File
Key settings include:
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
from datetime import datetime
project = "GitStats"
author = "Xianpeng Shen"
copyright = f"{datetime.now().year}, {author}"
html_theme = "sphinx_rtd_theme"If Markdown support is required, add the extension in conf.py :
extensions = [
'myst_parser', # support Markdown
]Configure ReadTheDocs Automatic Builds
1. Import Project to ReadTheDocs
Log in at https://readthedocs.org/ .
Click “Import a Project” and select your GitHub or GitLab repository.
Ensure the repository contains docs/conf.py ; the system will detect it automatically.
2. Add .readthedocs.yml Configuration File
Place the following file at the project root to control the build process:
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
version: 2
build:
os: ubuntu-24.04
tools:
python: "3.12"
sphinx:
configuration: docs/source/conf.py
python:
install:
- requirements: docs/requirements.txtAfter configuration, each Pull Request triggers an automatic pull and build, providing an up‑to‑date preview of the documentation.
Final Result
When the build finishes, ReadTheDocs provides a URL such as https://your-project.readthedocs.io/ for easy team collaboration and user reference.
The author’s open‑source project “GitStats” uses this setup as an example.
Conclusion
With the above configuration, you can achieve “write documentation, push, and it goes live” almost automatically, greatly improving documentation maintenance efficiency. If you are writing documentation for an open‑source or team Python project, give the Sphinx + ReadTheDocs combo a try.
DevOps Engineer
DevOps engineer, Pythonista and FOSS contributor. Created cpp-linter, commit-check, etc.; contributed to PyPA.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.