在Sphinx中,如何包括位于模块中但类和方法之外的文档字符串/注释

Sha*_*tan 5 documentation python-2.7 python-sphinx autodoc

utils我的包装中有一个模块。它由几个不需要实例化的杂项独立方法组成。

我想在此utils文件中放置一些通用注释/文档字符串,例如:

import os
import json

"""
Miscellaneous methods that help in <<blah blah>>
Does not require explicit instantiation.

The following actions can be performed:
===============   ===============
Action            Method
===============   ===============
Get a             :meth:`methoda`
Get b             :meth:`methodb`
"""

def methoda(data):
    "Gets A ..."
    ...

def methodb(data):
    "Gets B ..."
    ...
Run Code Online (Sandbox Code Playgroud)

如上所示,文档字符串具有一个包含指向各个方法的链接的表。目前,我的index.rst这部分内容包括utils

Utilities
============
.. automodule:: packagename.utils
   :members:
Run Code Online (Sandbox Code Playgroud)

目前,我得到了文档中正确显示的各个方法的文档字符串,但未获取模块的顶级文档字符串(任何类或方法之外)。使狮身人面像包含上述内容的最佳方法是什么?

一种选择是将顶级文档字符串移动到该文件之外,例如,移至index.rstetc。但是我宁愿不这样做,并将其保留在源文件中。

Sha*_*tan 8

感谢 jonsharpe 的评论,并引导我找到了执行此操作的正确方法。

为了供其他人将来参考,我基本上将文档字符串移动到文件的开头:

"""
Miscellaneous methods that help in <<blah blah>>
Does not require explicit instantiation.

The following actions can be performed:
===============   ===============
Action            Method
===============   ===============
Get a             :meth:`methoda`
Get b             :meth:`methodb`
"""

import os
import json

def methoda(data):
    "Gets A ..."
    ...

def methodb(data):
    "Gets B ..."
    ...
Run Code Online (Sandbox Code Playgroud)

就是这样!一切正常。