Autodoc是Sphinx的一个扩展程序,在构建时查看.rst文件中的automodule指令引用,导入和识别Python代码,然后将其docstring转换为html。
由于您的模块没有通过setup.py
安装到环境中,
因此需要手动导入您的模块,所以您需要在conf.py
中为sphinx提供上下文:
import os
import sys
sys.path.insert(0, os.path.abspath('./../..'))
在这种情况下,您的顶级模块的绝对路径比conf.py文件高2个层级。
完成后,您可以将您的autodoc指令文件arteryfe.rst添加回index.rst toctrees,然后它就会显示出来了。
Welcome to artery.fe's documentation!
=====================================
.. toctree::
:maxdepth: 2
:caption: Contents:
arteryfe
getting_started
tutorial
theory
numerical
如果您想进行环境安装,您将需要勾选选项以使ReadTheDocs使用虚拟环境并利用site-packages。
附录:
这是另一种方法,如果您有多个软件包,则尤其有用。
在更大的代码库中手动创建带有Autodoc指令的文件可能会有问题,因此我们有了Sphinx Apidoc - 它是补充Autodoc扩展的一个扩展。
这意味着您可以使用首选选项运行sphinx-apidoc,它将从您的Docstrings生成.rst文件,并生成为html的automodule指令 - 但这也可以在RTD的构建期间通过conf.py
完成。
例如,这将使Sphinx在构建时生成一个automodule arteryfe.rst
文件在/source/_autogen
中:
import os
import sys
sys.path.insert(0, os.path.abspath('./../..'))
import sphinx.apidoc
def setup(app):
sphinx.apidoc.main(['-f',
'-T',
'-E',
'-o',
'./source/_autogen/',
'./../arteryfe',
]
)
接下来只需将自动生成的所有输出添加到目录树中即可。
Welcome to artery.fe's documentation!
=====================================
.. toctree::
:maxdepth: 2
:caption: Contents:
getting_started
tutorial
theory
numerical
.. toctree::
:maxdepth: 2
:glob:
:caption: Code:
_autogen/*
由于为 apidoc 制作模板很复杂,因此它的灵活性稍有一点不足。但在某些情况下(例如大型代码库),仍然是一个有效的解决方案并且非常有用。
我在这里涂鸦了一个支持多个 apidoc 包的 RTD 兼容示例。