【发布时间】:2020-11-05 12:30:43
【问题描述】:
我有一个使用 Sphinx 文档的 Python 项目。我正在 readthedocs.io 服务上远程构建文档。
我使用了sphinx-quickstart,它生成了一个index.rst 文件,页脚中有这些链接:
Indices and tables
~~~~~~~~~~~~~~~~~~
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
当我将更改推送到 readthedocs.io 并构建文档时,我的构建成功。我通过toctree 指令手动链接的文档都可以正常工作。
search 链接可以正常工作。
但是genindex 链接指向一个空白页面,标题为“索引”
而modindex 页面链接到py-modindex.html,这是一个404。
按照本指南:https://samnicholls.net/2016/06/15/how-to-sphinx-readthedocs 我已经运行 sphinx-apidoc -o api-docs/ ../myproject 来生成 autodoc .rst 文件。
我将生成的api-docs/modules.rst 链接到我的index.rst 顶部的toctree 部分...该链接有效,如果我点击api-docs,则已正确生成。
sphinx-autodoc 也为我的项目中的每个包生成了文件,它们包含如下指令:
myproject.whatever module
-------------------------
.. automodule:: myproject.whatever
:members:
:undoc-members:
:show-inheritance:
如果我直接浏览到这些页面,它们有内容,但它们不会出现在索引中(只有它们手动链接的目录)。
我还有一些手动创建的页面,再次通过 toc 链接。
我的docs/conf.py 看起来像:
import os
import sys
sys.path.insert(0, os.path.abspath("../"))
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.viewcode",
"sphinx.ext.napoleon",
"sphinx_autodoc_typehints",
]
templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
html_theme = "alabaster"
html_static_path = ["_static"]
我相信,从 autodoc .rst 存根文件生成的 html 中填充了从我的项目中的 .py 文件中提取的模块和类,这表明 sys 路径修复和 autodoc 基本上可以正常工作。
所以我的问题是:
- 如何让
:ref:`genindex`有内容? - 如何修复
:ref:`modindex`指向不存在的py-modindex.html?
【问题讨论】:
-
sphinx-autodoc不会生成.rst文件,这是由sphinx-apidoc完成的。这里有 2 个主要可能性,1º 您在 conf.py 中设置了一些内容以不生成索引 for example html_use_index。或者,2º 你以某种方式破坏了你的 Sphinx(索引可能由于间接原因停止工作,来自其他地方的错误)。第一个包括你的conf.py第二个我建议生成一个最小的测试项目,一切都默认。 -
我没有使用
sphinx-apidoc,我没有在我的conf.py中设置html_use_index=False,默认为True -
根据您的描述,索引应该可以工作(这就是问题所在)!!所以问题的原因在别处。编辑您的问题以包含
conf.py(只是为了确保)并尝试创建一个新项目,其中包含1 个.py模块和1 个.rst,除了modules.rst和index.rst由sphinx-quickstart生成。另外,每次在make html之前运行make clean! -
嗯,我认为问题是 readthedocs.io 特定的......如果我在本地
make clean和make html然后生成一个py-modindex.html文件并且genindex.html有内容 -
您之前没有提到 RTD(老实说,我不知道可能存在什么问题)。但是这个问题在本地被
make clean解决了,因为没有清理的连续构建之间的不一致会破坏索引。
标签: python-sphinx restructuredtext read-the-docs autodoc