[Python] Sphinx can't see function inside of my class when it builds

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a project with this structure:

    model_monitoring/
    |-docs/
    |-src/
    | |-model_monitoring/
    | |-class_name1/
    | |-__init__.py
    | |-class_name1.py
    | |-class_name2/
    | |-__init__.py
    | |-class_name2.py


    I have to make a documentaion about every class of this project (only the file __init__.py inside every folder). I'm using sphinx with autodoc, autosummary and napoleon extiension.

    The problem, i think, is in the apidoc command, when i create the .rst file for every class. Then when i make the build it gives me a lot of errors related to missing module, like this:

    • Failed to import data_drift.data_drift.
    • Possible hints:
    • ModuleNotFoundError: No module named 'data_drift'
    • KeyError: 'data_drift'
    • Failed to import data_drift.
    • Possible hints:
    • ModuleNotFoundError: No module named 'data_drift'
    • KeyError: 'data_drift'Failed to import data_drift.data_drift.
    • Possible hints:
    • ModuleNotFoundError: No module named 'data_drift'
    • KeyError: 'data_drift'

    This error for every module.

    The structure of the .rst files:

    model\_monitoring.data\_drift package
    =====================================

    Submodules
    ----------

    model\_monitoring.data\_drift.data\_drift module
    ------------------------------------------------

    .. automodule:: model_monitoring.data_drift.data_drift
    :members:
    :undoc-members:
    :show-inheritance:

    Module contents
    ---------------

    .. automodule:: model_monitoring.data_drift
    :members:
    :undoc-members:
    :show-inheritance:


    conf.py

    import os
    import sys
    sys.path.insert(0, os.path.abspath('../src/model_monitoring/'))

    project = 'Documentazione Model Monitoring'
    copyright = '2025, Team AIS'
    author = 'Team AIS'
    release = '0.1'

    extensions = ['sphinx.ext.autodoc','sphinx.ext.autosummary','sphinx.ext.napoleon']

    templates_path = ['_templates']
    exclude_patterns = []

    html_theme = 'classic'
    html_static_path = ['_static']


    In _template/autosummary/ we have the 3 files:

    module.rst

    {{ fullname | escape | underline}}

    .. automodule:: {{ fullname }}

    {% block attributes %}
    {% if attributes %}
    .. rubric:: {{ _('Module Attributes') }}

    .. autosummary::
    :toctree: {{ fullname }}
    :nosignatures:
    {% for item in attributes %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block functions %}
    {% if functions %}
    .. rubric:: {{ _('Functions') }}

    .. autosummary::
    :toctree: {{ fullname }}
    :nosignatures:
    {% for item in functions %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block classes %}
    {% if classes %}
    .. rubric:: {{ _('Classes') }}

    .. autosummary::
    :toctree: {{ fullname }}
    :nosignatures:
    {% for item in classes %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block exceptions %}
    {% if exceptions %}
    .. rubric:: {{ _('Exceptions') }}

    .. autosummary::
    :toctree: {{ fullname }}
    :nosignatures:
    {% for item in exceptions %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block modules %}
    {% if modules %}
    .. rubric:: Modules

    .. autosummary::
    :toctree:
    :recursive:
    {% for item in modules %}
    {{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}


    class.rst

    {{ fullname | escape | underline}}

    .. currentmodule:: {{ module }}

    .. autoclass:: {{ objname }}

    {% block attributes %}
    {% if attributes %}
    .. rubric:: {{ _('Attributes') }}

    .. autosummary::
    {% for item in attributes %}
    ~{{ name }}.{{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}

    {% block methods %}
    {% if methods %}
    .. rubric:: {{ _('Methods') }}

    .. autosummary::
    :toctree:
    :nosignatures:
    {% for item in methods %}
    ~{{ name }}.{{ item }}
    {%- endfor %}
    {% endif %}
    {% endblock %}


    base.rst

    {{ fullname | escape | underline}}

    .. currentmodule:: {{ module }}

    .. auto{{ objtype }}:: {{ objname }}



    I changed the configuration in conf.py, added manually every functions of the class in the clann_name.rst file and used 3 different files for template to autocreate the .rst files with no resolution of the problem.

    Continue reading...

Compartilhe esta Página