How to document Python code using Doxygen [closed]

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?

Asked by: Julia953 | Posted: 28-01-2022

Answer 1

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.

Answered by: Lucas256 | Posted: 01-03-2022

Answer 2

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.

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():

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?

Answered by: Leonardo784 | Posted: 01-03-2022

Answer 3

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.

Answered by: Brianna614 | Posted: 01-03-2022

Answer 4

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.

Answered by: Edgar682 | Posted: 01-03-2022

Answer 5

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

Answered by: Stuart152 | Posted: 01-03-2022

Similar questions

documentation - how to document a python package

I know what's the standard way to document functions, classes and modules, but how do I document packages - do I put a docstring in, or something else?

documentation - Python: Document results and figures into tex document

I currently have written a script that produces several figures. I was wondering if there is a way to publish these figures directly into a tex file, say in eps format without including all of the python code verbatim. I also want to include the values of some variables. I looked at the module pweave ( but I couldn't figure out a way to ex...

documentation - How to document a Python variable?

How can I document a variable in Python. In JavaScript / JSDoc I can do something like that.: /** @type {Array<Number>} */ var foo; /** @type {Number[]} */ var bar; Some IDEs than can give better code completion. Is this also possible in Python?

documentation - How to document a class method in python

I'm looking for a good convention for documenting class methods so that when you look at the definition you can immediately tell what class the method belongs to. For example, if the class definition is 1000 lines long, then how can we clearly show that it belongs to the class. def f(self, x): pass Ideally, there is a solution that comes from a standard. A possible solution is to mimic...

documentation - "Interfaces" in Python: Yea or Nay?

Closed. This question is opinion-based. It is not c...

data structures - Producing documentation for Python classes

Closed. This question does not meet Stack Overflow guid...

syntax - "Slicing" in Python Expressions documentation

I don't understand the following part of the Python docs: Is this referring to list slicing ( x=[1,2,3,4]; x[0:2] )..? Particularly the parts referring to ellipsis.. slice_item ::= expression | proper_slice | ellipsis ...

documentation - How do I specify input and output data types in python comments?

I have seen several standards for writing comments about the kind of data a function expects and returns in Python. Is there a consensus on which one is best-practice? Is the new functionality in something I should start using for this?

python - Is there any pywin32 odbc connector documentation available?

What is a good pywin32 odbc connector documentation and tutorial on the web?

Where can I find good python Twisted framework documentation, blog entries, articles, etc?

Closed. This question does not meet Stack Overflow guid...

python - Qt-style documentation using Doxygen?

Closed. This question does not meet Stack Overflow guid...

What is the proper procedure for offering a patch to the Python documentation?

I'm about to dive into the source code for the module again because the MiniFieldStorage class is mentioned in the documentation, but not actually documented. It occurred to me that I have done this so many times that maybe I could write documentation for it. If I did, how should I submit it?

Python C extension: method signatures for documentation?

I am writing C extensions, and I'd like to make the signature of my methods visible for introspection. static PyObject* foo(PyObject *self, PyObject *args) { /* blabla [...] */ } PyDoc_STRVAR( foo_doc, "Great example function\n" "Arguments: (timeout, flags=None)\n" "Doc blahblah doc doc doc."); static PyMethodDef methods[] = { {"foo", foo, METH_VARARGS, foo_doc}, {NULL}, }; ...

python - ODFPy documentation

Closed. This question does not meet Stack Overflow guid...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python