繁体   English   中英

Python:在单个模块上使用 Sphinx 生成自动文档?

[英]Python: Generate autodoc with Sphinx on a single module?

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

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

但它只是说:

../cffiwrap.py is not a directory.

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

手册上说:

sphinx-apidoc [选项] -o packagedir [路径名...]

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

所以尝试:

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

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

这篇简短的博客文章似乎展示了一种为单个模块生成自动 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

暂无
暂无

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

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