使用 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
Using sphinx to auto-document a python class, module
提问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 Seg
in 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.rst
looks 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 Seg
is just printed in bold, and not linked to an autogenerated documentation of the class.
但Seg
只是以粗体打印,并没有链接到该类的自动生成的文档。
Trying:
See :class:Seg
Module :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:segments
Module
--------------------------
编辑:将 SegLib 更改为段(感谢 iElectric!),并将chapter1.rst 更改为::mod: segments
Module ------------------------- ——
.. 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.
顺便说一句:模块名称应该是小写。