简体   繁体   English

Function 在 Sphinx autodoc 生成的文档中出现两次

[英]Function appears two times in Sphinx autodoc generated documentation

Background information背景资料

I generate documentation from docstrings via Sphinx autodoc.我通过 Sphinx autodoc 从文档字符串生成文档。 There is a function mypackage.mypackage.foo() located in source file mypackage/mypackage.py .源文件mypackage/mypackage.py中有一个 function mypackage.mypackage.foo() It is imported implicite in the __init__.py , so the user can it use as mypackage.foo() .它在__init__.py中隐式导入,因此用户可以将其用作mypackage.foo()

I do manipulate Sphinx autodoc that it generates the docu for foo() respecting they way it is imported.我确实操作了 Sphinx autodoc,它根据导入的方式为foo()生成文档。 This works.这行得通。

The problem问题

The docu about foo() is generated twice;关于foo()的文档生成了两次; once as mypackage.foo() and once as mypackage.mypackage.foo() .一次作为mypackage.foo()一次作为mypackage.mypackage.foo()

在此处输入图像描述

Details细节

This is the structure of the project:这是项目的结构:

mypackage
├── a.py
├── __init__.py
└── mypackage.py

The __init__.py does the import and then manipulates the __all__ variable: __init__.py执行import ,然后操作__all__变量:

from .mypackage import *
__all__ = ['foo']  # for alternative see: https://stackoverflow.com/a/66996523/4865723

And the mypackage/mypackage.py defines foo() . mypackage/mypackage.py定义了foo()

Because of that I can do things like that:正因为如此,我可以做这样的事情:

import mypackage
mypackage.foo()

Related questions and answers相关问答

Here are questions and answers that took me to the current "part solution".以下是将我带到当前“部分解决方案”的问题和答案。 Where mypackage.foo() appears as expected in the documentation but mypackage.mypackage.foo() does not disappear. mypackage.foo()按预期出现在文档中,但mypackage.mypackage.foo()不会消失。

@mzjn gave me the right direction. @mzjn 给了我正确的方向。 He is correct that there are two automodule directives.他是正确的,有两个automodule指令。 So the goal is to make sphinx-apidoc (which generates the directives) ignore mypackage/mypackage.py .所以目标是让sphinx-apidoc (生成指令)忽略mypackage/mypackage.py

For that make it a "private module" with modyfing it's filename starting with an underscore ( _ ).为此,使它成为一个“私有模块”,修改它的文件名以下划线 ( _ ) 开头。

The resulting project structure:由此产生的项目结构:

mypackage
├── a.py
├── __init__.py
└── _mypackage.py

And the import in __init__.py need to modified to from._mypackage import * .__init__.py中的 import 需要修改为from._mypackage import *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM