繁体   English   中英

Sphinx 自动记录项目

[英]Sphinx Auto-documentation of Project

我正在关注 Python 项目的 Sphinx 教程,但似乎无法将自动创建的 HTML 文件放入函数 doc-strings 中。

我按照入门网站Sphinx 入门指南上的说明操作,并成功创建了一个具有基础知识(大部分为空)的 HTML。

以下是我的测试项目的结构:

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

__init__.py为空, api.py有一个函数:

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

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

我导航到doc/目录并运行$sphinx-quickstart

以下是我如何回答$sphinx-quickstart问题:

> 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

我在conf.py构建文件中进行了一项更改,以便 Sphinx 可以向上导航到项目的一个目录。 这是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'),
]

然后我运行$make html

正如预期的那样,这生成了一个index.html文件,但它不包含有关我的square_num()文档字符串的任何信息。 我没有编辑任何其他sphinx-quickstart默认文件。

我应该怎么做才能获得文档中的文档字符串?

编辑:

这不是如何使用 Sphinx 以零配置生成 Python 文档的重复

该问题中的具体解决方案不起作用。 conf.py第 3 行中,我已经通过添加以下行来实现该解决方案: sys.path.insert(0, os.path.abspath('../')) 虽然问题相同,但解决方案略有不同。

我找到的解决方案是在该行之后: sys.path.append(os.path.join(os.path.dirname(__name__), '..')) (见答案)

经过大量的跟踪和错误后,我发现编辑conf.py文件如下有效:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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