[Python] Relative imports in Python 3

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

  1. Stack

    Stack Membro Participativo

    I want to import a function from another file in the same directory.

    Usually, one of the following works:

    from .mymodule import myfunction


    from mymodule import myfunction


    ...but the other one gives me one of these errors:

    ImportError: attempted relative import with no known parent package


    ModuleNotFoundError: No module named 'mymodule'


    SystemError: Parent module '' not loaded, cannot perform relative import


    Why is this?

    Continue reading...

Compartilhe esta Página