函数在 Sphinx autodoc 生成的文档中出现两次
背景信息
我通过 Sphinx autodoc 从文档字符串生成文档。源文件 mypackage/mypackage.py
中有一个函数 mypackage.mypackage.foo()
。它在 __init__.py
中隐式导入,因此用户可以将其用作 mypackage.foo()
。
我确实操作了 Sphinx autodoc,它生成了 foo()
的文档,尊重它们的导入方式。这有效。
问题
关于foo()
的文档生成了两次;一次为 mypackage.foo()
,一次为 mypackage.mypackage.foo()
。
详细信息
这是项目的结构:
mypackage
├── a.py
├── __init__.py
└── mypackage.py
__init__.py
执行导入
,然后操作__all__
变量:
from .mypackage import *
__all__ = ['foo'] # for alternative see: https://stackoverflow.com/a/66996523/4865723
以及mypackage/mypackage.py
定义 foo()
。
因此,我可以做这样的事情:
import mypackage
mypackage.foo()
相关问题和解答
以下是带我进入当前“部分解决方案”的问题和答案。其中 mypackage.foo()
按文档中的预期显示,但 mypackage.mypackage.foo()
不会消失。
Background information
I generate documentation from docstrings via Sphinx autodoc. There is a function mypackage.mypackage.foo()
located in source file mypackage/mypackage.py
. It is imported implicite in the __init__.py
, so the user can it use as mypackage.foo()
.
I do manipulate Sphinx autodoc that it generates the docu for foo()
respecting they way it is imported. This works.
The problem
The docu about foo()
is generated twice; once as mypackage.foo()
and once as 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:
from .mypackage import *
__all__ = ['foo'] # for alternative see: https://stackoverflow.com/a/66996523/4865723
And the mypackage/mypackage.py
defines 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@mzjn 给了我正确的方向。他是正确的,有两个
automodule
指令。因此,我们的目标是让 sphinx-apidoc(生成指令)忽略 mypackage/mypackage.py。为此,使其成为“私有模块”,并修改其文件名以下划线(
_
)开头。由此产生的项目结构:
并且
__init__.py
中的导入需要修改为from ._mypackage import *
。@mzjn gave me the right direction. He is correct that there are two
automodule
directives. So the goal is to makesphinx-apidoc
(which generates the directives) ignoremypackage/mypackage.py
.For that make it a "private module" with modyfing it's filename starting with an underscore (
_
).The resulting project structure:
And the import in
__init__.py
need to modified tofrom ._mypackage import *
.