I have the following directory structure:
py_test
├── __init__.py
├── dir1
│ ├── __init__.py
│ └── script1.py
└── dir2
├── __init__.py
└── script2.py
In script2
I want to "import ..\script1
".
What I tried in script2
:
Does not work
from ..dir1 import script1 ImportError: attempted relative import with no known parent package`
Works
import sys, os path2add = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'dir1'))) if (not (path2add in sys.path)) : sys.path.append(path2add)
If I want to go with option 1, what is the simplest (i.e., with the least files) file/dir structure that makes it work?
I am aware of this, but I wonder if creating that directory structure can be avoided, and still use type-1 import
.
I am currently using this workaround, which uses type-2 import
.
Related:
How to import a Python class that is in a directory above?
Import a module from a directory (package) one level up
Using importlib to dynamically import module(s) containing relative imports