Autodocumenting Python using Sphinx

18,616

Solution 1

We use

.. automodule:: module
   :members:

Solution 2

To make things easier you can use this script (look at the bottom of the page for the last version): http://bitbucket.org/birkenfeld/sphinx/issue/98/add-the-autogenerate-script-to-sphinx

This script will parse your packages/modules and generate all the rest files necessary to build the doc from docstrings.

I'm the original author of this script.

UPDATE

This script is now part of Sphinx 1.1 as apidoc.

Solution 3

Etienne's script, mentioned in his answer, has now been integrated into Sphinx as sphinx-apidoc. It does exactly what the OP wants. It is slated for release in Sphinx 1.1, or is available from the Hg repo:

https://bitbucket.org/birkenfeld/sphinx

It works beautifully for me. The docs read thus:

> sphinx-apidoc --help
Usage: sphinx-apidoc-script.py [options] -o <output_path> <module_path>
           [exclude_paths, ...]

Look recursively in <module_path> for Python modules and packages and create
a reST file with automodule directives per package in the <output_path>.
Share:
18,616
Adam Matan
Author by

Adam Matan

Team leader, developer, and public speaker. I build end-to-end apps using modern cloud infrastructure, especially serverless tools. My current position is R&amp;D Manager at Corvid by Wix.com, a serverless platform for rapid web app generation. My CV and contact details are available on my Github README.

Updated on June 06, 2022

Comments

  • Adam Matan
    Adam Matan about 2 years

    This is a generalized version of a previous question regarding Sphinx.

    Is there a way to recursively autodocument modules or packages which contain classes and functions within them?

    I think it is silly to add the autofunction or automodule directive for each function; There must be a way to automate the process, otherwise I don't see the point of using Sphinx at all.

    Clarification: Instead of :

    .. automodule:: segments.segments
    
        .. autoclass:: segments.segments.Seg
    
            .. automethod:: Seg.method_1
    
            .. automethod:: Seg.method_2
    
            .. automethod:: Seg.method_3
    
            .......
    
            .. automethod:: Seg.method_n
    

    Which requires me to manually cut-and-paste all method names and update the documentation correspondingly, I want to have a command like:

    .. automodule:: segments.segments
    
        .. autoclass:: segments.segments.Seg
    
            .. MAGIC COMMAND: Automatically print the docstrings and signatures 
               of all Seg() methods.