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!
Relative imports can't be used by a top-level script (the script invoked with
python my_script.py
), even if that top-level script is nominally in a package, because the__package__
special variable is None for the top-level script.For more explanation see:
Relative imports for the billionth time
Import from another file inside the same module and running from a main.py outside the module throws an import error [duplicate]
Python sibling relative import error: 'no known parent package'
relative python imports. What is the difference between single dot and no dot
If your project structure has to be this way, and you actually want all of those files in the same package by having that top level
__init__.py
file in the root directory, then there are some solutions that come to mind:Import script1 from a main/app script outside of root. Create an app.py (or similar) file in the directory one level above root. This script should
import root
, and perhaps specificallyimport root.Dir1.Dri2.Dir3.Dir4.script1
, then run the primary function within script1.Reconsider the structure of this project. This may actually be an XY Problem where the issue that relative imports are not necessary here at all, but not enough information is available. If Dir1 and Dir5 aren't actually supposed to be part of the same package, then there should not be an
__init__.py
at root.If Dir1 and Dir5 are meant to be two separate packages, where Dir1 is dependent on Dir5, then perhaps Dir5 should be developed separately, and then installed to local site-packages using
pip -e
so that the package in Dir1 can import it using absolute ipmorts.