简体   繁体   中英

Sphinx Auto-documentation of Project

I'm following a Sphinx tutorial for a python project and cannot seem to get the auto-created HTML file to put in the function doc-strings.

I followed the instructions on the getting started site Sphinx Getting Started Guide , and successfully created an HTML that had the basics (mostly empty).

Here is how my test-project is structured:

- /sphinx-test/
  -- doc/
  -- sphinx-test/
    --- __init__.py
    --- api.py

__init__.py is empty, and api.py has one function in it:

def square_num(num):
    """Example function
    Args:
        num (float): A float to square.

    Returns:
        float: The squared number
    """
    return num**2

I navigated into the doc/ directory and ran $sphinx-quickstart .

Here is how I answered the $sphinx-quickstart questions:

> Root path for the documentation [.]: 
> Separate source and build directories (y/n) [n]: y
> Name prefix for templates and static dir [_]: 
> Project name: sphinx_test
> Author name(s): nick
> Project version: 0.0.1
> Project release [0.0.1]: 
> Project language [en]: 
> Source file suffix [.rst]: 
> Name of your master document (without suffix) [index]: 
> Do you want to use the epub builder (y/n) [n]: 
> autodoc: automatically insert docstrings from modules (y/n) [n]: y
> doctest: automatically test code snippets in doctest blocks (y/n) [n]: y
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]: n
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]: y
> coverage: checks for documentation coverage (y/n) [n]: y
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]: n
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: n
> ifconfig: conditional inclusion of content based on config values (y/n) [n]: y
> viewcode: include links to the source code of documented Python objects (y/n) [n]: y
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]: n
> Create Makefile? (y/n) [y]: y
> Create Windows command file? (y/n) [y]: n

I made one change in the conf.py build file so that Sphinx can navigate up one directory to the project. Here is the relevant lines in the conf.py :

# -*- coding: utf-8 -*-
import os
import sys
sys.path.insert(0, os.path.abspath('../'))
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.doctest',
    'sphinx.ext.todo',
    'sphinx.ext.coverage',
    'sphinx.ext.viewcode',
]
templates_path = ['_templates']
source_suffix = '.rst'
master_doc = 'index'
project = u'sphinx_test'
copyright = u'2019, foobar'
author = u'foobar'
version = u'0.0.1'
release = u'0.0.1'
language = None
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
pygments_style = 'sphinx'
todo_include_todos = True
html_theme = 'alabaster'
html_static_path = ['_static']
htmlhelp_basename = 'sphinx_testdoc'

latex_elements = {
}

latex_documents = [
    (master_doc, 'sphinx_test.tex', u'sphinx\\_test Documentation',
     u'foobar', 'manual'),
]

man_pages = [
    (master_doc, 'sphinx_test', u'sphinx_test Documentation',
     [author], 1)
]

texinfo_documents = [
    (master_doc, 'sphinx_test', u'sphinx_test Documentation',
     author, 'sphinx_test', 'One line description of project.',
     'Miscellaneous'),
]

I then ran $make html .

This generated an index.html file, as expected, but it does not contain any information on my square_num() docstring. I have not edited any other sphinx-quickstart default file.

What should I do to get the doc-strings in the documentation?

EDIT:

This is not quite a duplicate of How to generate Python documentation using Sphinx with zero configuration?

The specific solutions in that question did not work. In line 3 of the conf.py I had already implemented that solution by adding the line: sys.path.insert(0, os.path.abspath('../')) . While the problem was the same, the solutions were slightly different.

The solution I found was to after that line: sys.path.append(os.path.join(os.path.dirname(__name__), '..')) (See answer)

After a decent amount of trail and error, I found that editing the conf.py file as follows worked:

import os
import sys
sys.path.insert(0, os.path.abspath('../'))
sys.path.append(os.path.join(os.path.dirname(__name__), '..'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM