Python:在单个模块上使用Sphinx生成autodoc?

时间:2013-09-09 16:32:19

标签: python documentation python-sphinx autodoc

我正在开发一个只有一个.py模块的Python库,我正在尝试从docstrings为它生成文档。我已经设置了Sphinx并运行了spinx-quickstart脚本,但是当我尝试运行时(在docs目录中)

sphinx-apidoc ../cffiwrap.py -o .

但它只是说:

../cffiwrap.py is not a directory.

是否有其他Sphinx脚本可以自动修改单个文件?我想过只针对..运行它,但后来我发现它会运行到我的tests目录并尝试从我的单元测试中生成文档......

2 个答案:

答案 0 :(得分:0)

手册说:

  

sphinx-apidoc [options] -o packagedir [pathnames ...]

     

其中路径名是要排除的目录。

所以试试:

sphinx-apidoc -o . .. ../test_dir 

其中test_dir是您的测试所在的位置。

答案 1 :(得分:0)

简短的blog post似乎显示了为单个模块生成自动api文档的另一种方法。为方便和持久而复制在这里:

conf.py文件放置在模块所在的目录中

import os
import sys

# enable autodoc to load local modules
sys.path.insert(0, os.path.abspath("."))

project = "<project>"
copyright = "year, author"
author = "author"
extensions = ["sphinx.ext.autodoc", "sphinx.ext.intersphinx"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
templates_path = ["_templates"]
html_theme = "alabaster"
html_static_path = ["_static"]
intersphinx_mapping = {
    "python": ("https://docs.python.org/3", None)
}
html_theme_options = {"nosidebar": True}

在此index.rst旁边添加

<project>
=========

.. automodule:: <project>
   :members:

最后,将<project>替换为模块名称pip install sphinx并运行:

sphinx-build -b html . _build