如何使用Sphinx记录Python软件包

13

我正在尝试记录一个Python包。目前,我的目录结构如下:

.
└── project
    ├── _build
    │   ├── doctrees
    │   └── html
    │       ├── _sources
    │       └── _static
    ├── conf.py
    ├── index.rst
    ├── __init__.py
    ├── make.bat
    ├── Makefile
    ├── mod1
    │   ├── foo.py
    │   └── __init__.py
    ├── mod2
    │   ├── bar.py
    │   └── __init__.py
    ├── _static
    └── _templates

这棵树是sphinx-quickstart的结果。在conf.py中,我取消了注释:sys.path.insert(0, os.path.abspath('.'))并且我有extensions = ['sphinx.ext.autodoc']

我的index.rst如下:

.. FooBar documentation master file, created by
   sphinx-quickstart on Thu Aug 28 14:22:57 2014.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to FooBar's documentation!
==================================

Contents:

.. toctree::
   :maxdepth: 2

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
在所有的__init__.py文件和foo.py以及bar.py模块中,我都有一个文档字符串。但是,在项目中运行make html命令后,我看不到任何文档字符串。

你只有一个顶级的index.rst文件,但这还不够。你需要运行sphinx-apidoc来生成所需的.rst源文件(或者手动创建它们)。 - mzjn
Sphinx需要包含指令(如automoduleautoclass)的.rst文件才能生成API文档。如果没有这些指令,它就无法从源代码中提取信息。也许您期望Sphinx像Epydoc或Doxygen一样工作,但实际上并非如此。请参阅以下答案:https://dev59.com/y3E95IYBdhLWcg3wI6bt#2441159,https://dev59.com/YG025IYBdhLWcg3wRTxK#6109098。 - mzjn
1
让我们在聊天中继续这个讨论。点击此处进入聊天室 - Dror
1
刚刚用PyCharm写了一个完整的工作示例,可以用作灵感。https://dev59.com/C6Xja4cB1Zd3GeqPP1u0#46362065 - Ferhat
1个回答

10

这是一个大纲:

  1. 使用源代码中的文档字符串记录您的软件包。
  2. 使用sphinx-quickstart创建Sphinx项目。
  3. 运行sphinx-apidoc生成.rst源文件,以便与autodoc一起使用。更多信息请查看此处

    使用该命令并带有-F标志还会创建完整的Sphinx项目。如果您的API经常发生变化,则可能需要多次运行此命令。

  4. 使用sphinx-build构建文档。

注意事项:


1
我应该在哪里运行sphinx-apidoc?如何处理生成的.rst文件?将它们放在哪里?使用-Fspinx-quickstart有什么不同? - minerals
@minerals。我应该在哪里运行sphinx-apidoc?你所说的“哪里”是指什么?有什么问题吗?生成的.rst文件应该怎么处理?放在哪里?你可以将它们放在方便的地方,然后在上面运行sphinx-build - mzjn
1
@minerals。使用-F选项与sphinx-quickstart有何不同? 如果你运行sphinx-apidoc -F,实际上你是在一次操作中运行了sphinx-quickstartsphinx-apidoc。这只是另一种做法。 - mzjn
@minerals。关于sphinx-apidoc:我在我的回答中链接了http://sphinx-doc.org/man/sphinx-apidoc.html。那个页面上有什么不清楚的地方吗?另外请注意,如果您有一个稳定的API,则不必一遍又一遍地运行`sphinx-apidoc`。请参见https://dev59.com/0Yfca4cB1Zd3GeqPns75。 - mzjn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接