[Python] Is there a way to enforce module imports based on __init__.py?

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to write a function that enforces the imports that are stated in the __init__.py.

    For example, my dummy project would look something like this:

    project/
    __init__.py
    src/
    __init__.py
    module_a/
    __init__.py
    a0.py
    a1.py
    module_b/
    __init__.py
    b0.py


    And from a0,a1 only those parts could be imported that are stated in module_a's __init__.py, let them be from .a0 import ... or __all__ = [...]. How can one approach such a function? I'm trying to extract the imports from __init__ files with ast, and match them the imports inside the other .py files.

    Continue reading...

Compartilhe esta Página