Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Documenting your Python library, from zero to website

You can read this on Medium here.

Documentation is quite a project, but your project is already a project, and who wants another project? Here we will try to describe how to document your project, all in one go. We will start with a totally empty project, and go all the way to a website hosted on GitHub pages, built automatically with GitHub actions.

You can see the final result here.

You can find all the code for this project on GitHub here.

Your library

I said we'd do it from scratch, so we will have to make a library. You probably already have a library, so you can easily figure out how to paste your files here.

Let's create a library called autos. Create a folder for it and two files:

mkdir autos
touch autos/
touch autos/

with contents for

class Bike:

    def __init__(self, name): = name

    def ride(self, road):
        print("Riding: %s on road: %s" % (, road))

        return True

and the file:

from .bike import *

Let's also add a test tests/ in a separate directory:

import autos

if __name__ == "__main__":
    b = autos.Bike(name="SR400")

Finally, let's make this a real package by including a You can use a generator, or just steal the default one from - create with contents:

import setuptools

# Load the long_description from
with open("", "r") as fh:
    long_description =

    description="A small example package",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",

Obviously, edit it as you see fit. Here you will also need a to serve as the long_description (and because) it's good practice.

The final directory structure should look like:


You should now be able to install the library

pip install .

The test script in the tests directory should produce:

Riding: SR400 on road: 101

Great! On to the documentation.

Adding documentation

Let's start by adding some documentation. If you are using VS Code (which you should be!), you can use the Python Docstring Generator to help you out.

First add typing declarations to all the method arguments and return values:

class Bike:

    def __init__(self, name: str): = name

    def ride(self, road : str) -> bool:
        print("Riding: %s on road: %s" %, road)

        return True

Some other common types are:

  • List, e.g. List[int].
  • Tuple, e.g. Tuple[str,float].
  • Dict, e.g. Dict[str,int].

Go under one of the method signatures and type """. It should suggest the documentation string: drawing

Then hit enter to autocomplete the documentation: drawing

Fill in some documentation, for example:

class Bike:

    def __init__(self, name: str):

            name (str): Bike name
        """ = name

    def ride(self, road : str) -> bool:

            road (str): Road to ride on

            bool: True if successful
        print("Riding: %s on road: %s" %, road)

        return True

The neat thing here is that editing the should in any modern IDE (e.g. VS Code) now give you autocomplete suggestions:


Generating the website

You need to install Sphinx:

pip install -U sphinx

Let's make a new docs folder:

mkdir docs
cd docs

Use sphinx-quickstart to get started:


Choose all the defaults, i.e. no for > Separate source and build directories (y/n) [n]:. For project name, I chose autos. You should now have:


The Makefile is obviously used to build the project. specifies the build, and index.rst is the entry point for the website.

To make the website, run:

make html

To make different targets, try just make to list all possible targets. The resulting pages will be in _build/html. The default page looks like this:


I always like the ReadTheDocs theme. Go ahead and install it:

pip install sphinx-rtd-theme

Let's add it to the configuration in Add the import statement, and add the rest:

import sphinx_rtd_theme

extensions = [

html_theme = "sphinx_rtd_theme"

Now we get:


That looks better! But we don't yet have our doc strings we wrote in Python appearing.

Autogenerate RST files

Converting doc strings into documentation is done using the sphinx.ext.autodoc extension as described here. Go ahead and add it to the extensions list in

extensions = [

In order for specific classse to appear in the documentation, there must be an RST file referencing them. An example snippet in a .rst file might look like this:

.. automodule::

which shows that the class should be documented here.

Let's automatically generate them. From inside the docs directory, run:

sphinx-apidoc -o . ../autos

Here -o . specifies that the current docs directory is the output, and the ../autos specifies the source directory. We obtain two new files:


with contents for autos.rst:

autos package

---------- module

.. automodule::

Module contents

.. automodule:: autos

and for modules.rst contains a running list of the modules:


.. toctree::
   :maxdepth: 4


Finally, we need to connect these new files to the original index.rst. Add a reference to the modules.rst in index.rst:

Welcome to autos's documentation!

.. toctree::
   :maxdepth: 2
   :caption: Contents:


Indices and tables

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

Fire up the make command again:

make html

to obtain the following:


This almost worked. We notice that for the class documentation, we are missing the docstrings for the constructor!


For some brilliant reason, the __init__ method is skipped by default! Let's fix that using this trick. Add the following snippet to at the bottom:

def skip(app, what, name, obj, would_skip, options):
    if name == "__init__":
        return False
    return would_skip

def setup(app):
    app.connect("autodoc-skip-member", skip)

Rebuild, making sure clean first:

make clean html
make html

and you should find the constructor is now documented:


Hosting your documentation with GitHub

Start by initializing a git repo with git init . for the folder which has contents:


You should also use a .gitignore with at least:


Commit the remaining files in the repo, then go on GitHub, create a new repo for this project and push your first commit.

git add .
git commit -m "Initial"
git remote add origin
git push --set-upstream origin master

Let's create a new GitHub Actions workflow. They live in a .github/workflows directory that does not yet exist. Create it:

mkdir .github/
mkdir .github/workflows
touch .github/workflows/docs.yml
open .github/workflows/docs.yml

Give the file the following contents:

name: docs
    - master

    name: Docs
    runs-on: ubuntu-latest

    - uses: actions/checkout@v2

    - name: Install Python
      uses: actions/setup-python@v1
        python-version: 3.8

    - name: Install requirements
      run: |
        pip3 install sphinx-rtd-theme
    - name: Build docs
      run: |
        cd docs
        make html

    - name: Deploy
      if: success()
      uses: peaceiris/actions-gh-pages@v3
        publish_branch: gh-pages
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: docs/_build/html/

Let's break it down:

  • name - obvious
  • on - you can add different triggers here. Here we build the docs on every push to master. You can also do e.g. pull request.
  • docs job - this is the only job here, which will build the docs.
  • runs-on: ubuntu-latest - You can choose from a couple differet runners other than ubuntu-latest as described here.
  • uses: actions/checkout@v2 - very important! Without this, we don't actually check out our repo!
  • Install Python - get the latest 3.8 Python - ubuntu-latest is not-so-latest.
  • Build docs - the make html command from before. Make sure we do it in the docs directory.
  • Deploy - deploy the pages in docs/_build/html to the gh-pages branch such that your docs become visible to the world.

Add, commit and push the workflow. You can monitor the progress of the action on your repo page under Actions.

Your website should then be available under:

Mine didn't appear right away. Check your settings on the "Settings" tab under "GitHub pages" - they should automatically have changed to build from the gh-pages branch:


I had to manually change this from gh-pages to master and back to get the website to become available. It should then also automatically list the URL here.

You can also check your gh-pages branch, which should containt your HTML files:


A common problem in the build process is that you used some external library that you installed with pip install external_library. Now what - should you add it to the build process for the documentation? No - this only adds more dependencies that need to be installed, and it's probably not relevant for the docs. Instead you can edit in the option:

autodoc_mock_imports = [

such that the build process for the docs will proceed even when the library is missing.

Final thoughts

Your final docs are now online!


Finally, we can complete this guide by adding a badge to your that your docs built:



Thanks for reading!


Sphinx tutorial for documenting Python packages






No releases published


No packages published