自动生成所有 Python 包内容的文档

发布于 2024-10-10 20:15:25 字数 1769 浏览 11 评论 0 原文

我正在尝试使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

去了角落 2024-10-17 20:15:25

您可以尝试使用 sphinx-apidoc。

$ sphinx-apidoc --help
Usage: sphinx-apidoc [options] -o <output_path> <module_path> [exclude_paths, ...]

Look recursively in <module_path> for Python modules and packages and create
one reST file with automodule directives per package in the <output_path>.

您可以将 sphinx-apidoc 与 sphinx-quickstart 混合使用,以便创建整个文档项目,如下所示:

$ sphinx-apidoc -F -o docs project

此调用将使用 sphinx-quickstart 生成一个完整项目,并在 中递归查找; (项目)Python 模块。

You can try using sphinx-apidoc.

$ sphinx-apidoc --help
Usage: sphinx-apidoc [options] -o <output_path> <module_path> [exclude_paths, ...]

Look recursively in <module_path> for Python modules and packages and create
one reST file with automodule directives per package in the <output_path>.

You can mix sphinx-apidoc with sphinx-quickstart in order to create the whole doc project like this:

$ sphinx-apidoc -F -o docs project

This call will generate a full project with sphinx-quickstart and Look recursively in <module_path> (project) for Python modules.

森罗 2024-10-17 20:15:25

也许 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.

终弃我 2024-10-17 20:15:25

注意

对于Sphinx(实际上是执行的Python解释器)
Sphinx)来找到您的模块,它必须是可导入的。这意味着
模块或包必须位于以下目录之一
sys.path – 相应地在配置文件中调整您的sys.path

所以,转到你的 conf.py 并添加

import an_example_pypi_project.useful_1
import an_example_pypi_project.useful_2

现在你的 index.rst 看起来像:

.. toctree::
   :glob:

   example
   an_example_pypi_project/*

制作html

Note

For Sphinx (actually, the Python interpreter that executes
Sphinx) to find your module, it must be importable. That means that
the module or the package must be in one of the directories on
sys.path – adapt your sys.path in the configuration file accordingly

So, go to your conf.py and add

import an_example_pypi_project.useful_1
import an_example_pypi_project.useful_2

Now your index.rst looks like:

.. toctree::
   :glob:

   example
   an_example_pypi_project/*

and

make html

著墨染雨君画夕 2024-10-17 20:15:25

从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文