【发布时间】:2020-01-24 20:07:22
【问题描述】:
我在一个包含automodule 的rst 文件上运行Sphinx,但它似乎没有任何效果。
以下是详细信息:我有一个 Python 项目,其中有一个文件 agent.py,其中包含一个类 Agent。我还有一个子目录apidoc,里面有一个文件agent.rst(由sphinx-apidoc生成):
agent module
============
.. automodule:: agent
:members:
:undoc-members:
:show-inheritance:
我使用sphinx-build -b html apidoc apidoc/_build 运行 sphinx,并将项目目录作为当前工作目录。
为了确保找到 Python 文件,我在 apidoc/conf.py 中包含以下内容:
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
它运行时没有错误,但是当我打开生成的 HTML 文件时,它只显示“代理模块”并且一切都是空白的。为什么它不显示类 Agent 及其成员?
更新:最初的问题可能是因为我没有在conf.py 中包含sphinx.ext.autodoc。但是,现在我这样做了,我收到如下警告:
WARNING: invalid signature for automodule ('My Project.agent') WARNING: don't know which module to import for autodocumenting 'My Project.agent' (try placing a "module" or "currentmodule" directive in the document, or giving an explicit module name) WARNING: autodoc: failed to import module 'agent'; the following exception was raised: No module named 'agent'
【问题讨论】:
-
您的模块必须在 Python 包中。将其放入一个包含
__init__.py的文件夹中,并根据需要调整conf.py和您的reST,例如mypackage.agent。 -
我不知道这里有什么问题,但是模块必须在一个包中是不正确的。可以有一个
agent.py模块而没有__init__.py。 -
确实,这似乎没有什么不同。我已对问题添加了更新,请查看。
标签: python python-sphinx autodoc sphinx-apidoc