Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate Python documentation using Sphinx with zero configuration?

We don't want to be maintaining documentation as well as the source code, which is evolving rapidly at the moment, yet Sphinx seems to require a frustrating amount of setup and configuration. (We just need some basic API docs.) Is there not a single command you can run inside a python project that will just iterate over all of the packages, modules, classes and functions generating documentation as HTML?

The sphinx-apidoc splats stuff into a directory, and after modifying the conf.py to have our packages in the sys.path we can run "make html", but it only lists packages and modules without documenting any classes or functions.

Thanks!

like image 206
rosejn Avatar asked Feb 26 '13 13:02

rosejn


People also ask

How to add Sphinx documentation to Python code?

Should the documentation in your code follow the Google Python Style Guide, you’ll need to append sphinx.ext.napoleon to the extensions list. 4. Auto-generate the rst Files Sphinx generates the HTML documentation from reStructuredText (rst) files.

What does conf Py do in Sphinx?

The conf.py file inside the source folder describes the Sphinx configuration, which controls how Sphinx builds the documentation. If you wish to override the theme, version, or module directory, you’ll need to override these changes here.

How to adjust Docutils configuration in Sphinx?

An optional file docutils.conf can be added to the configuration directory to adjust Docutils configuration if not otherwise overridden or set by Sphinx.

What is the “extensions” variable for in Sphinx?

The extensions variable is assigned to a list of extensions needed to build the documentation. For instance, if you’re planning to include documentation from your doc using the autodoc directives, you’ll need to activate it by adding sphinx.ext.autodoc to the extension list. This, of course, is optional depending on the preferred docstring format.


1 Answers

The sphinx-apidoc tool will autogenerate stubs for your modules, which might be what you want.

Instructions

  • Make sure the autodoc module was enabled during Sphinx configuration.

    extensions = ['sphinx.ext.autodoc']
    

    within Sphinx's conf.py should do the trick.

  • Make sure conf.py adjusts sys.path accordingly (see the comments at lines 16-19 in the file).

    sys.path.insert(0, os.path.abspath('/my/source/lives/here'))
    
  • Run sphinx-apidoc to generate skeletons.

    sphinx-apidoc -o /my/docs/live/here /my/source/lives/here
    
  • Rebuild the docs. If all goes well, you shouldn't get the following sort of warning:

    mymodule.rst:4: WARNING: autodoc can't import/find module 'mymodule'

  • Your module RSTs should now be populated.

like image 71
AKX Avatar answered Oct 22 '22 00:10

AKX