Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - How to use sphinx automodule and exposed functions in __init__

I have a folder structure that looks like:

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

When I use the module directly it seems to be working as expected:

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

However, when I go to use the automodule in sphinx (after adding the project folder to my sys.path):

.. automodule:: mymod
    :members:

.. automodule:: mymod.sub1
    :members:

I get a page with just:

The __init__ docstr
The sub1 __init__ docstr

It seems to be ignoring the from m1 import * type statements...

But, if I copy all of the code directly into the __init__ files for each module/sub-module I get:

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

Does sphinx automodule not work with modules with these sorts of import statements in the __init__ file or am I missing a more obvious issue?

Ideally, I'd like an output like I get when putting all the code in the init (put using import statements instead).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It works if you add an __all__ list to the __init__.py files. For example:

"""The __init__ docstr"""

__all__ = ['func1', 'func2']

from m1 import *
from m2 import *

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...