代码之家  ›  专栏  ›  技术社区  ›  buhtz

在Sphinx autodoc中使用模块文档字符串

  •  0
  • buhtz  · 技术社区  · 4 年前

    在Sphinx autodoc生成的文档中,我希望每个模块都有一个介绍文本。我的逻辑这与模块docstring有关。但是Sphinx忽略了它,因为它在生成的HTML文件中没有出现。

    # -*- coding: utf-8 -*-
    import mypackage
    
    """This is the modules docstring.
    """
    
    
    def bar(bar):
        """
        This is the function named bar.
    
        Parameters:
            bar (str): Just a parameter.
    
        Returns:
            str: Just an 'a'.
        """
        mypackage.foo(bar)
        return('a')
    

    我不确定这是否在斯芬克斯的概念中,或者斯芬克斯是否希望我以另一种方式意识到这一点。

    但重点是我不想,但文件 rst -文件。每个文档内容都应该来自 py -文件本身。

    0 回复  |  直到 4 年前
        1
  •  0
  •   buhtz    4 年前

    基于@mzjn的评论。

    模块docstring必须是模块中的第一个东西(之前 import 语句)。另请参阅: https://stackoverflow.com/a/48682589/407651

    所以它一定是

    # -*- coding: utf-8 -*-
    """This is the modules docstring.
    """
    import mypackage
    
    def bar(bar):
    # ...
    ´´´