103

I like Doxygen to create documentation of C or PHP code. I have an upcoming Python project and I think I remember that Python doesn't have /* .. */ comments, and also has its own self-documentation facility which seems to be the pythonic way to document.

Since I'm familiar with Doxygen, how can I use it to produce my Python documentation? Is there anything in particular that I need to be aware of?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Hanno Fietz
  • 30,799
  • 47
  • 148
  • 234

5 Answers5

93

The doxypy input filter allows you to use pretty much all of Doxygen's formatting tags in a standard Python docstring format. I use it to document a large mixed C++ and Python game application framework, and it's working well.

Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125
75

This is documented on the doxygen website, but to summarize here:

You can use doxygen to document your Python code. You can either use the Python documentation string syntax:

"""@package docstring
Documentation for this module.

More details.
"""

def func():
    """Documentation for a function.

    More details.
    """
    pass

In which case the comments will be extracted by doxygen, but you won't be able to use any of the special doxygen commands.

Or you can (similar to C-style languages under doxygen) double up the comment marker (#) on the first line before the member:

## @package pyexample
#  Documentation for this module.
#
#  More details.

## Documentation for a function.
#
#  More details.
def func():
    pass

In that case, you can use the special doxygen commands. There's no particular Python output mode, but you can apparently improve the results by setting OPTMIZE_OUTPUT_JAVA to YES.

Honestly, I'm a little surprised at the difference - it seems like once doxygen can detect the comments in ## blocks or """ blocks, most of the work would be done and you'd be able to use the special commands in either case. Maybe they expect people using """ to adhere to more Pythonic documentation practices and that would interfere with the special doxygen commands?

albert
  • 8,285
  • 3
  • 19
  • 32
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • 62
    Comments as documentation in Python is bad. Comments are for a module maintainer (why and how implemented). All documentation should be in docstrings (how to use). – jfs Sep 13 '08 at 21:20
  • 4
    Python will pull in the comments and use them as docstrings, so the two formats both work with pydoc. – docwhat Feb 03 '11 at 16:47
  • 11
    Have a look at [doxypy](http://code.foosel.org/doxypy) which makes it possible to use the _special commands_ inside normal docstrings. – Mauro Oct 27 '11 at 03:48
  • 1
    Take a look at [doxypypy](https://github.com/Feneric/doxypypy) - An updated implementation of doxypy – Jay M Feb 23 '22 at 17:21
  • 1
    Use """! ... """, and doxygen will also format correctly – Mathy Mar 31 '22 at 07:16
28

In the end, you only have two options:

You generate your content using Doxygen, or you generate your content using Sphinx*.

  1. Doxygen: It is not the tool of choice for most Python projects. But if you have to deal with other related projects written in C or C++ it could make sense. For this you can improve the integration between Doxygen and Python using doxypypy.

  2. Sphinx: The defacto tool for documenting a Python project. You have three options here: manual, semi-automatic (stub generation) and fully automatic (Doxygen like).

    1. For manual API documentation you have Sphinx autodoc. This is great to write a user guide with embedded API generated elements.
    2. For semi-automatic you have Sphinx autosummary. You can either setup your build system to call sphinx-autogen or setup your Sphinx with the autosummary_generate config. You will require to setup a page with the autosummaries, and then manually edit the pages. You have options, but my experience with this approach is that it requires way too much configuration, and at the end even after creating new templates, I found bugs and the impossibility to determine exactly what was exposed as public API and what not. My opinion is this tool is good for stub generation that will require manual editing, and nothing more. Is like a shortcut to end up in manual.
    3. Fully automatic. This have been criticized many times and for long we didn't have a good fully automatic Python API generator integrated with Sphinx until AutoAPI came, which is a new kid in the block. This is by far the best for automatic API generation in Python (note: shameless self-promotion).

There are other options to note:

  • Breathe: this started as a very good idea, and makes sense when you work with several related project in other languages that use Doxygen. The idea is to use Doxygen XML output and feed it to Sphinx to generate your API. So, you can keep all the goodness of Doxygen and unify the documentation system in Sphinx. Awesome in theory. Now, in practice, the last time I checked the project wasn't ready for production.
  • pydoctor*: Very particular. Generates its own output. It has some basic integration with Sphinx, and some nice features.
Havok
  • 5,776
  • 1
  • 35
  • 44
  • What is the command to use autoapi. I have modified the conf.py to include autoapi modules. Currently, I run "sphinx-apidoc -o apidocs --full ." – Kabira K Feb 16 '17 at 01:01
  • You don't need a an extra command. Just build your Sphinx documentation using sphinx-build. I have it integrated with Tox like this: https://github.com/HPENetworking/cookiecutter_python/blob/module/%7B%7Bcookiecutter.repo_name%7D%7D/tox.ini#L30 – Havok Feb 20 '17 at 07:47
  • @Havok I don't see how _AutoAPI_ is "Fully automatic" when I have to put all elements of a module into the `__all__` variable explicite. – buhtz Oct 01 '18 at 21:06
  • Doxygen is a documentation generator for C++, for Sphinx the code generation is just an option, the main purpose of Sphinx is to **write documentation**. The integration with Python is far from the one with C++. e.g.1 All python docstring of a project need to be rewriten with specific tags if you want to use them on doxygen. e.g. 2 https://www.doxygen.nl/manual/diagrams.html "Doxygen has built-in support to generate inheritance diagrams for C++ classes." but not for Python, even less for function calls. – user3313834 Mar 01 '21 at 10:40
22

Sphinx is mainly a tool for formatting docs written independently from the source code, as I understand it.

For generating API docs from Python docstrings, the leading tools are pdoc and pydoctor. Here's pydoctor's generated API docs for Twisted and Bazaar.

Of course, if you just want to have a look at the docstrings while you're working on stuff, there's the "pydoc" command line tool and as well as the help() function available in the interactive interpreter.

JPaget
  • 969
  • 10
  • 13
Allen
  • 5,034
  • 22
  • 30
  • 2
    It is true, that sphinx uses docs written independently from source code as a base, but using the autodoc extension one can easily include docstrings from python modules. Because of its dynamic nature I find hand written documentation for python modules more useful than generated api docs. – Peter Hoffmann Sep 13 '08 at 07:22
  • 3
    "Hand-written" docs are not available when you're trying to grok the structure and relationship between classes in some hardly-documented project. – Ярослав Рахматуллин Jun 28 '13 at 19:21
  • pdoc is used by a commandline one-liner (without other config files) and seems totally fine to me. We generate docs for python with pdoc when building projects on CI. – kaiser Jan 26 '21 at 07:10
14

An other very good documentation tool is sphinx. It will be used for the upcoming python 2.6 documentation and is used by django and a lot of other python projects.

From the sphinx website:

  • Output formats: HTML (including Windows HTML Help) and LaTeX, for printable PDF versions
  • Extensive cross-references: semantic markup and automatic links for functions, classes, glossary terms and similar pieces of information
  • Hierarchical structure: easy definition of a document tree, with automatic links to siblings, parents and children
  • Automatic indices: general index as well as a module index
  • Code handling: automatic highlighting using the Pygments highlighter
  • Extensions: automatic testing of code snippets, inclusion of docstrings from Python modules, and more
creyD
  • 1,972
  • 3
  • 26
  • 55
Peter Hoffmann
  • 56,376
  • 15
  • 76
  • 59
  • 12
    Have tried it. While sphinx in itself is a very interesting tool, it was not what I expected coming from doxygen. More a tool for really good end customer doc, doxygen is much better for a SW designer who just would like to see an API overview in a nice printable format. – Zane Jun 02 '14 at 15:27
  • 3
    @Zane I do agree. As a Doxygen lover I missed way too much the fully automatic API reference guide generation. Check my answer as some other option is now available. – Havok Feb 13 '16 at 08:26