Relative path explained



../project_root/
     bin/relative_path_explained.py
     lib/my_module.py


examples/project_root/bin/relative_path_explained.py
import os
import sys

print(__file__)
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))

project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(project_root)

mypath = os.path.join(project_root, 'lib')
print(mypath)
sys.path.insert(0, mypath)

import my_module
my_module.run()

examples/project_root/bin/relative_path_explained.py
/home/gabor/work/slides/python/examples/project_root/bin/relative_path_explained.py
/home/gabor/work/slides/python/examples/project_root/bin
/home/gabor/work/slides/python/examples/project_root
/home/gabor/work/slides/python/examples/project_root/lib
Hello from my_module