如何在 __init__ 中使用 sphinx automodule 和暴露的函数

How to use sphinx automodule and exposed functions in __init__

我的文件夹结构如下所示:

project/
    mymodule/
        __init__.py
        m1.py
        m2.py
        sub1/
            __init__.py
            s1.py
            s2.py

mymod/__init__.py:

"""The __init__ docstr"""
from m1 import *
from m2 import *

mymod/m1.py:

"""m1 doc str"""
def func1():
    """func1 docstr"""
    return 1

mymod/m2.py:

"""m2 doc str"""
def func2():
    """func2 docstr"""
    return 2

mymod/sub1/__init__.py:

"""The sub1 __init__ docstr"""
from s1 import *
from s2 import *

mymod/sub1/s1.py:

"""s1 docstr"""
def sub1():
    """sub1 docstr"""
    return 3

mymod/sub1/s2.py:

"""s2 docstr"""
def sub2():
    """sub2 docstr"""
    return 4

当我直接使用该模块时,它似乎按预期工作:

>>> import mymod
>>> from mymod import sub1
>>> mymod.func1()
1
>>> sub1.subfunc1()
3
>>> mymod.__doc__
'The __init__ docstr'
>>> mymod.func1.__doc__
'func1 docstr'

但是,当我去使用 sphinx 中的自动模块时(将项目文件夹添加到我的 sys.path 之后):

.. automodule:: mymod
    :members:

.. automodule:: mymod.sub1
    :members:

我得到的页面只有:

The __init__ docstr
The sub1 __init__ docstr

似乎忽略了 from m1 import * 类型语句...

但是,如果我将所有代码直接复制到每个 module/sub-module 的 __init__ 文件中,我会得到:

The __init__ docstr

mymod.func1()
func1 docstr

mymod.func2()
func2 docstr

The sub1 __init__ docstr

mymod.sub1.sub1()
sub1 docstr

mymod.sub1.sub2()
sub2 docstr

sphinx automodule 是否无法与 __init__ 文件中包含此类导入语句的模块一起使用,还是我遗漏了一个更明显的问题?

理想情况下,我希望得到的输出与将所有代码放入 init(改为使用 import 语句)时得到的输出一样。

如果您将 __all__ 列表添加到 __init__.py 文件,它会起作用。例如:

"""The __init__ docstr"""

__all__ = ['func1', 'func2']

from m1 import *
from m2 import *