1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python - import function from parallel directory

Discussão em 'Python' iniciado por Stack, Agosto 31, 2025.

  1. Stack

    Stack Membro Participativo

    Let's assume I have two parallel directories, with simple python files.

    • 'dir_one' with file_one.py
    • 'dir_two' with file_two.py

    In file_one.py there is a function named function_one. How can I import it in file_two.py?

    I have checked literally a few dozens of sources, including answers on stackoverflow. Therefore, I am not quoting any of them here. I have tried four different solutions, none of them worked:

    1. Insert 'init.py' files in all the directories - results in error 'ImportError: attempted relative import with no known parent package'
    2. Make both directories a single package - not possible in this case, directories have to be able to run as independent apps.
    3. Use sys module: sys.path.insert(0, 'path-to-dir_one') right before 'import dir_one' line - doesn't change anything, still get 'ModuleNotFoundError: No module named 'dir_one'.
    4. try relative import: 'from .dir_one import file_one' - 'ModuleNotFoundError: No module named 'zweizen.prodalitics'.

    Is it possible at all to import a function in python from parallel folder without making it part of the same package?

    Continue reading...

Compartilhe esta Página