Skip to main content
Background Image
  1. Posts/

Step by Step Guide to Autogenerate Python Documentation With Sphinx

·3 mins·
Yalchin Mammadli
Author
Yalchin Mammadli
Experienced Python developer with 6+ years of expertise in Django, Flask, and FastAPI, specializing in high-performance web applications. Skilled in Node.js/TypeScript (Express), Docker, CI/CD, and TDD with 95% test coverage. Proven track record across ERP, CRM, e-commerce, blockchain, and AI projects. Focused on continuous growth and delivering impactful, scalable solutions.
Table of Contents

Creating Autogenerated Sphinx Documentation for Python Projects
#

Sphinx is a powerful documentation generator that makes it easy to create intelligent and beautiful documentation for Python projects. In this guide, we’ll walk through the process of setting up Sphinx and using it to autogenerate documentation from your Python source code.

Setting Up the Environment
#

First, let’s create a virtual environment and install the necessary packages:

# Create a virtual environment
python -m venv venv

# Activate the virtual environment
# On Windows:
venv\Scripts\activate
# On macOS and Linux:
source venv/bin/activate

# Install Sphinx and the Furo theme
pip install sphinx furo

Initializing Sphinx
#

Now that we have Sphinx installed, let’s initialize our documentation:

# Create a docs directory
mkdir docs
cd docs

# Run sphinx-quickstart
sphinx-quickstart

During the quickstart process, you’ll be asked several questions. Here are some recommended answers:

> Separate source and build directories (y/n) [n]: n
> Project name: test-project
> Author name(s): Your Name
> Project release []: 0.0.1
> Project language [en]: en

After running sphinx-quickstart, your directory structure should look like this:

docs/
├── build/
├── source/
│   ├── conf.py
│   ├── index.rst
│   └── _static/
└── make.bat (on Windows) or Makefile (on Unix)

Configuring Sphinx
#

Now, let’s configure Sphinx by editing the conf.py file in the source directory. Here’s an example configuration:

# Configuration file for the Sphinx documentation builder.

import os
import sys

sys.path.insert(0, os.path.abspath("../src"))

# -- Project information
project = 'test-project'
copyright = '2024, Your Name'
author = 'Your Name'
release = '0.0.1'

# -- General configuration
extensions = [
    "sphinx.ext.napoleon",
    "sphinx.ext.autodoc",
    "sphinx.ext.viewcode",
    "celery.contrib.sphinx",
]
templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# -- Options for HTML output
html_theme = 'furo'
html_static_path = ['_static']

Let’s break down the extensions we’ve added:

  • sphinx.ext.napoleon: Supports Google and NumPy style docstrings.
  • sphinx.ext.autodoc: Automatically includes documentation from docstrings.
  • sphinx.ext.viewcode: Adds links to the source code of documented Python objects.
  • celery.contrib.sphinx: Adds support for Celery-specific directives (only needed if you’re using Celery).

Please see the extra imports and path insert made in the very first lines, it will add the directory of interest to the path otherwise documentation you have generated can end up being empty. Make sure you do necessary changes as per your changes

Autogenerating Documentation
#

Now, let’s use sphinx-apidoc to automatically generate documentation from your source code:

sphinx-apidoc -o docs/source src/ tests/

This command will create .rst files for each module in your src directory, excluding the tests directory.

Understanding the RST Files
#

modules.rst
#

The modules.rst file serves as a table of contents for your project’s modules:

test_project
============
.. toctree::
   :maxdepth: 4

   src

This file lists all the modules in your project, allowing Sphinx to create a hierarchical structure for your documentation.

index.rst
#

The index.rst file is the main entry point of your documentation:

.. test-project documentation master file, created by
   sphinx-quickstart on Fri Sep 13 10:09:31 2024.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

test_project documentation
==========================

Add your content using ``reStructuredText`` syntax. See the
`reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html>`_
documentation for details.

.. toctree::
   :maxdepth: 2

   modules

Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

This file defines the structure of your documentation’s main page. You can add more content or include additional .rst files in the toctree directive.

Building the Documentation
#

Finally, let’s build the documentation:

# Navigate to the docs directory
cd docs

# Build the HTML documentation
make html

Your generated documentation will be in the docs/build/html directory. Open the index.html file in a web browser to view your newly created documentation.

Conclusion
#

You’ve now set up Sphinx to autogenerate documentation for your Python project. Remember to keep your docstrings up-to-date and rebuild the documentation whenever you make significant changes to your code. Happy documenting!

Related

How to Secure Ipfs Node
·3 mins
Implementing Database Logs or History
·7 mins
Basic Vim Cheat Sheet
·3 mins
What Is Virtual Environment in Python and How to Set Up Virtual Environment
·4 mins
Sorting Algorithms and Binary Search in Python
·3 mins
Map Sort Filter Functions in Python
·2 mins