- __file__
- dirname
- abspath
- sys.path
Relative path
../project_root/ bin/relative_path.py lib/my_module.py
We can use a directory structure that is more complex than the flat structure we had earlier. In this case the location of the modules relatively to the scripts
is fixed. In this case it is "../lib". We can compute the relative path in each of our scripts. That will ensure we pick up the right module every time we run the script.
Regardless of the location of the whole project tree.
examples/project_root/lib/my_module.py
def run(): print("Hello from my_module")
examples/project_root/bin/relative_path.py
import os import sys project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(project_root, 'lib')) import my_module my_module.run()