Adding Numpydoc to Sphinx

This post is a continuation to the Sphinx Autodoc Tutorial for Dummies.

Sphinx has great documentation, but sometimes its documentation requirements can be somewhat awkward when looking at the code.

Here is a Sphinx function documentation example, which looks like this:

def public_fn_with_sphinxy_docstring(name, state=None):
    """This function does something.

    :param name: The name to use.
    :type name: str.
    :param state: Current state to be in.
    :type state: bool.
    :returns:  int -- the return code.
    :raises: AttributeError, KeyError


Not that intuitive, IMHO.

Numpy is an excellent Python package for scientific computing, and they also offer their own Sphinx extension for documentation, numpydoc, that 1) looks good in html, and 2) looks clear in code.

Here is a Numpydoc function documentation example, which looks like this:

def foo(var1, var2, long_var_name='hi')
    """This function does something.

    var1 : array_like
        This is a type.
    var2 : int
        This is another var.
    Long_variable_name : {'hi', 'ho'}, optional
        Choices in brackets, default first when optional.

    describe : type

I personally find the numpy version quite a bit more intuitive, and it looks great.


Super simple (easy_install reference).

easy_install numpydoc

And then add

extensions = ['numpydoc']

to your file. If you’re already using autodoc or other extensions, the line will look like this:

extensions = ['sphinx.ext.autodoc', 'numpydoc']

That’s it! Running

make html

should compile your documentation using numpydoc. If you don’t care for it – no problem – just remove the extension from

Sphinx Autodoc Tutorial for Dummies

I recently installed Sphinx and spent the better part of a day trying to get it to work. And not because it was hard (it actually came with a “d’oh!” moment at the end), but rather because I found the Sphinx tutorial rather obscure. This tutorial is going to show how to use Sphinx to autodocument your modules.

It’s actually really easy. Follow these steps, and you can’t go wrong.

Install Sphinx.

easy_install -U Sphinx

easy_install comes with the Python installation (2.7) and can be found in the Scripts subfolder (C:\Python27\Scripts). This is also where the sphinx-quickstart script will be added.

Create a documentation directory.

Get started by running


You’ll enter a number of options now. In most cases you can simply click ENTER, and Sphinx will select the default options. I’ve highlighted some things you want to pay attention to:

  • Root path for the documentation. Where you want your files to be. I created a “docs” subfolder in my main project, so it doesn’t clutter up my project.
  •  autodoc: automatically insert docstrings from modules (y/N) [n]: Click Yes Here
  • Be sure to create a makefile. It makes life simple.

You can pretty much go with the default options, other than autodoc. Sphinx will generate an empty project in the path you specified, with a default index.rst file. This will be the root of your project – all other files will be accessed through it. At this point, the basic structure already works. Compile it by running the makefile generated by the sphinx-quickstart script.

make html

The generated html files will be in your specified path/_build/html.

Autodoc, roll out!

In order for sphinx to be able to document your python code, it needs to find it first. Open the generated file in your specified path. Add the following line (with the path of your Python source). Make sure to use Python string formatting as shown!


You can also use the following notation for a relative directory. abspath will make the path absolute and fix up the formatting as well.


Adding notation

Open index.rst. The blank file should look like this:


.. toctree::
   :maxdepth: 2

Indices and tables

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

Add the name of one of your python modules and an example class, located in your specified source path. The index file should look like this:


.. toctree::
   :maxdepth: 2

.. automodule:: SomeModuleName

.. autoclass:: SomeClassName

Indices and tables

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

Run make html again to rebuild. Sphinx should automatically identify your module and add a reference to SomeModuleName and SomeClassName. These references will also appear in the index. The clause :members: specifies that all members of the class will be documented, so you don’t have to write in each function manually.

Expanding the Table of Contents

At this point you can add other files and modules to create the documentation the way you like. Make sure all additional files are in the format you specified (default is *.rst), and in your sphinx documentation directory. For instance, if you were to move your module documentation into a file called code.rst, the expanded index might look like this:


.. toctree::
   :maxdepth: 2


Indices and tables

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


Python modules / files don’t appear in the documentation.

  1. Sphinx documentation works with alignment. Make sure the indentation is exactly as shown in both previous examples, so that all the items in the table of contents are exactly aligned. This is an easy one to miss.
  2. Check the path in sys.path, maybe by printing it out yourself. Is the formatting correct? Is the path accurate?
  3. Have you typed in the module / class name correctly (case and all)?

I can see my class, but it doesn’t show any / some of the functions.

Function documentation should look like the example and be indented properly as well, otherwise sphinx won’t recognize it.

Created on 29 July 2012
@author: Lisa Simpson

class DatabaseManager(object):
    Create and manage a new sqlite database.

I don’t feel like putting in all my files manually!

Neither do I! The next section is for you…

Saving Time With APIdoc

APIDoc is a tool that comes with sphinx and is designed to automatically pull documentation out of your entire project, automatically creating *.rst files for each module. You can find the apidoc scripts in the Python2X/Scripts directory with the other sphinx scripts.

sphinx-apidoc [options] -o <outputdir> <sourcedir> [pathnames ...]

Options and pathnames are optional, but the full explanation of all the options can be found in the sphinx apidoc documentation. Apidoc is a simple script which instantly generates all your project documentation.


Tutorial expanded! Learn how to add numpydoc to Sphinx, and what it gives you…