Consider the following structure
├── __init__.py
├── script.py
├── lib
│ ├── __init__.py
│ ├── utils.py
│ ├── utils2.py
I know that I can import any function defined in utils.py
into script.py
doing from lib.utils import function
.
Also, I can import any function defined in utils2.py
into utils.py
using a relative import, i.e. from .utils2 import function2
.
With time my root folder has been growing with files so that an additional organization into folders is needed, so I would like to have something like this
├── __init__.py
├── realtime
│ ├── __init__.py
│ ├── script.py
├── lib
│ ├── __init__.py
│ ├── utils.py
│ ├── utils2.py
If I attempt an import with respect to the package root in realtime/script.py
now
from lib.utils import function
I get an error.
Is there any way to resolve the import always with respect to the root folder when I'm running a script inside a subfolder? I would like to avoid having all my main scripts in the root and also avoid having to insert something in syspath
, which is what many solutions online are suggesting. Does Python not have an official way of dealing with this?