I am trying to import methods from script2.py in script1.py
This is my current folder structure
I keep getting "ImportError: attempted relative import with no known parent package", and I've tried many variations of importing without any success.
What exactly do I need in my init.py(s) and what do I need in my script1.py import lines in order to make this work. I've seen other questions related to this, answered on much shallower file structures and the solutions do not seem to work when I try and modify them to fit mine.
root
└── __init__.py
├── Dir1
│ └── Dir2
│ └── Dir3
| └── Dir4
| └── __init__.py
| └── script1.py
└── Dir5
└── Dir6
└── __init__.py
└── script2.py
I've tried changing sys.path to the root directory and Dir6 before importing.
I've also tried from .......Dir5.Dir6 import script2
I've also tried using absolute paths like from root.Dir5.Dir6 import script2
I've also tried adding init.py to every directory
I greatly appreciate any help!
The
__init__.pyfile is needed in ALL directories for Python to "to make Python treat directories containing the file as packages." (Link)Futhermore, you need to be referencing relative paths lower in the dir struct that your
__main__contextTry creating more
__init__.pyfiles to achieve the follow dir struct:The
__init__.pyfiles are empty, and the other files contents are as follows:To run this example: