[Python] python import from sibling folder

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to import a module from a python file that is in a sibling folder. I read several similar questions here and tried to apply solutions listed there, but I wasn't able to solve the problem.

    The structure is as follows:

    parentfolder/gfolder/codefolder/fileA.py
    parentfolder/gfolder/utilfolder/util.py


    gfolder, codefolder and utilfolder all have an __init__.py.

    I'm trying to do in fileA.py:

    import gfolder.utilfolder.util as util


    I also tried adding before the import statement:

    sys.path.append(".../parentfolder/")


    And that didn't work either:

    import gfolder.utilfolder.util as util
    ModuleNotFoundError: No module named 'gfolder'


    The solution in a similar question says to include __init.py__ in the directories, which I already have.

    EDIT: Now both sys.append and sys.insert work and the problem was that I included a slash at the end of the path. When I took it out, everything worked.

    Continue reading...

Compartilhe esta Página