You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Sphinx文档中包含从其他模块导入的特定函数?

Great question! The issue here is that Sphinx's automodule directive, by default, only documents members that are defined directly in the module (like f1 in your __init__.py), not ones imported from other modules (like f2 from misc.py).

Here are three straightforward ways to fix this:

1. Use the :imported-members: flag

Add this flag to your automodule directive to tell Sphinx to include imported functions:

my_module
---------
.. automodule:: my_module
    :members:
    :undoc-members:
    :imported-members:

This will automatically pull in f2 since it's imported into my_module.

2. Explicitly list f2 in :members:

If you only want to include specific imported members, you can list them alongside your local functions:

my_module
---------
.. automodule:: my_module
    :members: f1, f2
    :undoc-members:

This directly tells Sphinx to document both f1 and f2, regardless of where they're defined.

3. Define __all__ in your __init__.py

If you want to declare the public API of your module, adding __all__ will make :members: automatically include all listed members:
Update your __init__.py to include:

from my_module.misc import f2
def f1(p1,p2):
    """ My f1 function.
    :param p1: First parameter.
    :param p2: Second parameter.
    :return: Returned value.
    """
    return p1 + p2

__all__ = ['f1', 'f2']

Now your original automodule directive will include both functions, as Sphinx respects the __all__ list when using :members: without explicit names.

All three methods will get f2 showing up in your Sphinx docs—pick the one that best fits how you want to manage your module's public API!

内容的提问来源于stack exchange,提问作者Krzysztof Słowiński

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:56:01