0

i have a file path that looks like this:

project_folder
  |--sub_folder
  |    |--file1.py
  |--file2.py

now in file1.py i would like to import file2.py but my attempts don`t work:

#this is what i tryed in file1.py:
from . import file2.py
from .. import file2.py
from ...project_folder import file2.py
from project_folder import file2.py

import ..file2.py
import project_folder.file2.py

everything is failing with: "ImportError: attempted relative import with no known parent package".

I`ve also tried to add a __init__.py file to the project_folder and the sub_folder but that did not help.

I`m not shure if anything has to be in these __init__.py files or if they can stay empty.

Nils
  • 275
  • 3
  • 12
  • 1
    Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Leonardus Chen Dec 07 '20 at 21:55
  • i've seen this already and i thought i did it that way. But obviously i'm missing something. – Nils Dec 07 '20 at 22:13

1 Answers1

0

for me the answer is: Just import the files that are inside and not outside a folder. If you import a file that is outside the folder it can break you Programm because you created an import loop.

So if you think you have to do a relative import you might have to rethink your file system.

Nils
  • 275
  • 3
  • 12