使用 sphinx 自动记录 Python 类、模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1326796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:57:48  来源:igfitidea点击:

Using sphinx to auto-document a python class, module

pythonpython-sphinxdocumentation-generationautodoc

提问by Adam Matan

I have installed Sphinxin order to document some python modules and class I'm working on. While the markup language looks very nice, I haven't managed to auto-document a python code.

我已经安装了Sphinx以记录我正在处理的一些 python 模块和类。虽然标记语言看起来很不错,但我还没有设法自动记录 Python 代码。

Basically, I have the following python module:

基本上,我有以下 python 模块:

SegLib.py

And A class called Segin it. I would like to display the docstrings of the class and module within the generated sphinx document, and add further formatted text to it.

Seg在其中调用了一个类。我想在生成的 sphinx 文档中显示类和模块的文档字符串,并向其中添加进一步的格式化文本。

My index.rstlooks like this:

我的index.rst看起来像这样:

Contents:

.. toctree::
:maxdepth: 2

chapter1.rst

and chapter1.rst:

chapter1.rst

This is a header
================
Some text, *italic text*, **bold text**

* bulleted list.  There needs to be a space right after the "*"
* item 2

.. note::
   This is a note.

See :class:`Seg`

But Segis just printed in bold, and not linked to an autogenerated documentation of the class.

Seg只是以粗体打印,并没有链接到该类的自动生成的文档。

Trying: See :class:SegModule :mod:'SegLib' Module :mod:'SegLib.py'

尝试:参见:class:Seg模块:mod:'SegLib'模块:mod:'SegLib.py'

Didn't help, too. Any ideas or good tutorial links?

也没有帮助。任何想法或好的教程链接?

Edit: changed SegLib to segments (thanks, iElectric!), and changed chapter1.rst to:The :mod:segmentsModule --------------------------

编辑:将 SegLib 更改为段(感谢 iElectric!),并将chapter1.rst 更改为::mod: segmentsModule ------------------------- ——

.. automodule:: segments.segments

.. autoclass:: segments.segments.Seg

Still, can't get sphinx to directly document functions within a class, or better - to automatically add all the functions within a class to the document. Tried

尽管如此,还是无法让 sphinx 直接记录类中的函数,或者更好 - 自动将类中的所有函数添加到文档中。试过

.. autofunction:: segments.segments.Seg.sid

and got:

并得到:

autodoc can't import/find function 'segments.segments.Seg.sid', it reported error: "No module named Seg"

Any ideas how to autodocument the functions and classes with a short command?

任何想法如何使用短命令自动记录函数和类?

Udi

乌迪

采纳答案by iElectric

Add to the begining of the file:

添加到文件的开头:

.. module:: SegLib

Try using :autoclass:directive for class doc.

尝试对类文档使用:autoclass:指令。

BTW: module names should be lower_case.

顺便说一句:模块名称应该是小写。

EDIT:I learned a lot from reading other source files.

编辑:我从阅读其他源文件中学到了很多东西