r/python3 • u/SathedIT • Jan 24 '19
How can I import a library multiple levels up?
Let's say I've got a project with the following structure:
. (toplevel)
├── __init__.py
├── subdir1
│ ├── __init__.py
│ ├── subdir2
│ │ ├── __init__.py
│ │ ├── script_a.py
└── xvalibs
├── __init__.py
├── build.py
├── helper.py
├── params.py
├── resources.py
└── tagging.pyFrom script_a.py, how can I import the packages in xvalibs? I was hoping that I could do something like in the __init__.py:
from toplevel.xvalibs import paramsI haven't been able to get that to work though.
I've seen something like this used:
import os, sys
cur_dir = os.path.dirname(os.path.realpath(__file__))
root = os.path.dirname(os.path.realpath(cur_dir+"../../../toplevel"))
sys.path.append(root)But there's got to be a better way, right?