Python: Best way to add to sys.path relative to the current running script

If you don't want to edit each file

  • Install you library like a normal python libray
    or
  • Set PYTHONPATH to your lib

or if you are willing to add a single line to each file, add a import statement at top e.g.

import import_my_lib

keep import_my_lib.py in bin and import_my_lib can correctly set the python path to whatever lib you want


This is what I use:

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))

I'm using:

import sys,os
sys.path.append(os.getcwd())