我正在尝试使用 Sphinx 为我的代码库自动生成基本文档。但是,我很难指示 Sphinx 递归扫描我的文件。
我有一个 Python 代码库,其文件夹结构如下:
<workspace>
└── src
└── mypackage
├── __init__.py
│
├── subpackageA
│ ├── __init__.py
│ ├── submoduleA1
│ └── submoduleA2
│
└── subpackageB
├── __init__.py
├── submoduleB1
└── submoduleB2
我在
中运行了 sphinx-quickstart,所以现在我的结构如下:
<workspace>
├── src
│ └── mypackage
│ ├── __init__.py
│ │
│ ├── subpackageA
│ │ ├── __init__.py
│ │ ├── submoduleA1
│ │ └── submoduleA2
│ │
│ └── subpackageB
│ ├── __init__.py
│ ├── submoduleB1
│ └── ubmoduleB2
│
├── index.rst
├── _build
├── _static
└── _templates
我已阅读 快速入门教程,虽然我仍在尝试理解这些文档,但它的措辞方式让我感到担忧Sphinx 假设我要为代码库中的每个模块/类/函数手动创建文档文件。
但是,我确实注意到了“automodule”声明,并且我在快速启动期间启用了 autodoc,所以我希望大部分文档可以自动生成。我修改了我的conf.py以将我的src文件夹添加到sys.path,然后修改我的index.rst以使用automodule。所以现在我的 index.rst 看起来像:
Contents:
.. toctree::
:maxdepth: 2
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
.. automodule:: alphabuyer
:members:
我在子包中定义了数十个类和函数。然而,当我运行:
sphinx-build -b html . ./_build
它报告:
updating environment: 1 added, 0 changed, 0 removed
这似乎未能导入我的包中的任何内容。查看生成的index.html 在“Contents:”旁边没有显示任何内容。 Index页面只显示“mypackage(模块)”,但点击它也显示没有任何内容。
如何指示 Sphinx 递归地解析包并自动为其遇到的每个类/方法/函数生成文档,而不必自己手动列出每个类?
I'm trying to auto-generate basic documentation for my codebase using Sphinx. However, I'm having difficulty instructing Sphinx to recursively scan my files.
I have a Python codebase with a folder structure like:
<workspace>
└── src
└── mypackage
├── __init__.py
│
├── subpackageA
│ ├── __init__.py
│ ├── submoduleA1
│ └── submoduleA2
│
└── subpackageB
├── __init__.py
├── submoduleB1
└── submoduleB2
I ran sphinx-quickstart in <workspace>
, so now my structure looks like:
<workspace>
├── src
│ └── mypackage
│ ├── __init__.py
│ │
│ ├── subpackageA
│ │ ├── __init__.py
│ │ ├── submoduleA1
│ │ └── submoduleA2
│ │
│ └── subpackageB
│ ├── __init__.py
│ ├── submoduleB1
│ └── ubmoduleB2
│
├── index.rst
├── _build
├── _static
└── _templates
I've read the quickstart tutorial, and although I'm still trying to understand the docs, the way it's worded makes me concerned that Sphinx assumes I'm going to manually create documentation files for every single module/class/function in my codebase.
However, I did notice the "automodule" statement, and I enabled autodoc during quickstart, so I'm hoping most of the documentation can be automatically generated. I modified my conf.py to add my src folder to sys.path and then modified my index.rst to use automodule. So now my index.rst looks like:
Contents:
.. toctree::
:maxdepth: 2
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
.. automodule:: alphabuyer
:members:
I have dozens of classes and functions defined in the subpackages. Yet, when I run:
sphinx-build -b html . ./_build
it reports:
updating environment: 1 added, 0 changed, 0 removed
And this appears to have failed to import anything inside my package. Viewing the generated index.html shows nothing next to "Contents:". The Index page only shows "mypackage (module)", but clicking it shows it also has no contents.
How do you direct Sphinx to recursively parse a package and automatically generate documentation for every class/method/function it encounters, without having to manually list every class yourself?
发布评论
评论(4)
您可以尝试使用 sphinx-apidoc。
您可以将 sphinx-apidoc 与 sphinx-quickstart 混合使用,以便创建整个文档项目,如下所示:
此调用将使用 sphinx-quickstart 生成一个完整项目,并在 中递归查找; (项目)Python 模块。
You can try using sphinx-apidoc.
You can mix sphinx-apidoc with sphinx-quickstart in order to create the whole doc project like this:
This call will generate a full project with sphinx-quickstart and Look recursively in <module_path> (project) for Python modules.
也许 apigen.py 可以提供帮助: https://github.com/nipy/nipy/tree /master/tools。
该工具的描述非常简单:http://comments.gmane.org/gmane.comp.python.sphinx.devel/2912。
或者更好的是,使用 pdoc。
更新: sphinx-apidoc 实用程序已添加在Sphinx 版本 1.1。
Perhaps apigen.py can help: https://github.com/nipy/nipy/tree/master/tools.
This tool is described very briefly here: http://comments.gmane.org/gmane.comp.python.sphinx.devel/2912.
Or better yet, use pdoc.
Update: the sphinx-apidoc utility was added in Sphinx version 1.1.
注意
所以,转到你的 conf.py 并添加
现在你的 index.rst 看起来像:
和
制作html
Note
So, go to your conf.py and add
Now your index.rst looks like:
and
make html
从 Sphinx 版本 3.1(2020 年 6 月)开始,如果您愿意使用 sphinx.ext.autosummary 来显示汇总表,则可以使用新的
:recursive:
选项自动检测包中的每个模块,无论嵌套有多深,并自动为该模块中的每个属性、类、函数和异常生成文档。在这里查看我的答案:https://stackoverflow.com/a/62613202/12014259
From Sphinx version 3.1 (June 2020), if you're happy to use
sphinx.ext.autosummary
to display summary tables, you can use the new:recursive:
option to automatically detect every module in your package, however deeply nested, and automatically generate documentation for every attribute, class, function and exception in that module.See my answer here: https://stackoverflow.com/a/62613202/12014259