简体   繁体   English

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

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

I'm working on a Python library that only has a single .py module and I'm trying to generate documentation for it from the docstrings.我正在开发一个只有一个 .py 模块的 Python 库,我正在尝试从文档字符串中为它生成文档。 I have Sphinx set up and ran through the spinx-quickstart script, but when I try to run (while in the docs directory)我已经设置了 Sphinx 并运行了 spinx-quickstart 脚本,但是当我尝试运行时(在 docs 目录中)

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

But it just says:但它只是说:

../cffiwrap.py is not a directory.

Is there some other Sphinx script to autodoc a single file?是否有其他一些 Sphinx 脚本可以自动记录单个文件? I thought about just running it against .. but then I figured it would run in to my tests directory and try to generate docs from my unit tests...我想只针对..运行它,但后来我认为它会运行到我的测试目录中并尝试从我的单元测试中生成文档......

The manual says:手册上说:

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

where pathnames are the directories to exclude.其中路径名是要排除的目录。

So try:所以尝试:

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

where test_dir is where your tests live.其中test_dir是您的测试所在的位置。

This short blog post seems to show an alternative way to generate an automatic api doc for a single module.这篇简短的博客文章似乎展示了一种为单个模块生成自动 api 文档的替代方法。 Copied here for convenience and persistence:为了方便和持久性,复制到这里:

Place the conf.py file at the same directory as your module: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}

Add this index.rst next to it在它旁边添加这个index.rst

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

.. automodule:: <project>
   :members:

Finally, replace <project> with your module name, pip install sphinx and run:最后,将<project>替换为您的模块名称, pip install sphinx并运行:

sphinx-build -b html . _build

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

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