[Python] Relative imports - ModuleNotFoundError: No module named x

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

  1. Stack

    Stack Membro Participativo

    This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files:

    1. test.py
    2. config.py

    config.py has a few functions defined in it as well as a few variables. I've stripped it down to the following:

    config.py

    debug = True


    test.py

    import config
    print (config.debug)


    I also have an __init__.py

    However, I'm getting the following error:

    ModuleNotFoundError: No module named 'config'


    I'm aware that the py3 convention is to use absolute imports:

    from . import config


    However, this leads to the following error:

    ImportError: cannot import name 'config'


    So I'm at a loss as to what to do here... Any help is greatly appreciated. :)

    Continue reading...

Compartilhe esta Página