0

I have the following file structure:

file_system

And I don't know why this piece of code doesn't work:

# this is in main.py
import sys
from pathlib import Path

main = Path(__file__).parents[2]
if main not in sys.path:
        sys.path.insert(0, main)
print(main)
from mainFolder.dir1.subdir1 import forImport

Comparing it to this Import module from subfolder I can't explain myself what I am doing wrong.

Thank you in advance!

stefan.stt
  • 2,357
  • 5
  • 24
  • 47

1 Answers1

3

your main.py is within a subfolder, the import statement doesn't search "siblings" of that folder, only scripts in the folder where main.py is and subfolders in the same folder

if your main.py was in the folder above mainFolder your statement would work

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62