3

我为我的 click 应用程序使用 sphinx-click 扩展名,我想使用 sphinx-apidoc仅为子模块生成 .rst 文件。

我已经使用 pythons Sphinx 文档库大约一年了,我有一个用例……我不太清楚……也许我现在只是瞎了眼。

无论如何,我有一个 cli 工具,其结构类似于

my_tool/
+-- __init__.py
+-- cli_entry.py
+-- utils
|   +-- __init__.py
|   +-- foo.py
|   +-- bar.py

其中 cli_entry 是一个点击应用程序,它导入my_tool.utils.foomy_tools.utils.bar 因为这是使用 Click 库。我决定使用sphinx_click扩展来记录其中的任何命令cli_entry.py(它记录了所有的命令)。

但这是问题所在,我想用它来为模块sphinx-apidoc中的所有内容生成 .rst 文件。./my_tool/utils/当我使用该命令作为sphinx-apidoc -o ../docs/utils my_tool/utils我得到的输出文件时

docs/
+-- utils
|   +-- module.rst
|   +-- utils.rst

一开始看起来不错,但打开utils.rst文件后看起来像

utils package
=============

Submodules
----------

utils.foo module
----------------------

.. automodule:: utils.foo
   :members:
   :undoc-members:
   :show-inheritance:

utils.bar module
----------------------

.. automodule:: utils.bar
   :members:
   :undoc-members:
   :show-inheritance:

然后,当我使用(来自 sphinx 生成的 makefile)构建文档时,make html我收到一条错误消息,说Failed to import 'utils.foo': no module named utils.foo那是因为导入应该以my_tool.utils.foo

如何使用sphinx-apidoc仅生成子模块包含正确的导入路径?也许这是我在conf.py.. 中缺少的东西。也许这是我从 sphinx-apidoc 中缺少的一个选项?

编辑:我应该提到我可以使用exclude_pattern参数......但我宁愿不必在我的根目录中指定每个 cli 文件。前任。在我有的情况下cli_entry.pycli_commandgroup1.py...... cli_commandgroupN.py我希望这个解决方案足够动态,只支持子模块。

编辑:我尝试使用sphinx-apidoc -o ../docs/utils my_tool/,这会创建以下输出

docs/
+-- utils
|   +-- module.rst
|   +-- my_tool.cli_entry.rst
|   +-- my_tool.utils.rst

现在在my_tool.utils.rst文件中,导入是正确的,并且可以生成文档。

my_tool.utils.foo module
----------------------

.. automodule:: my_tool.utils.foo
   :members:
   :undoc-members:
   :show-inheritance:

指定的问题my_tool/my_tool.cli_entry.rst,当这个 .rst 文件已经使用 click-sphinx 扩展名创建时,它会被创建。

4

0 回答 0