[Baypiggies] Locating the directory you are executing from

Jeff Younker jeff at drinktomi.com
Sun May 4 20:37:22 CEST 2008

I'm hoping that there is an easier way of solving this problem.  In
particular I'm hoping that there is a standard library call that I've

I have a set of development scripts.  The scripts can potentially
be executed from many places.  They reference information which
is relative to their installation paths.  (These are build scripts.)

The CI system can supply a fixed root, but when run by the developers
I'd like them to be runnable from anywhere in the project, and still  
them work.

The current incantation I'm using is:

def bin_dir():
     this_file = sys.modules[__name__].__file__
     return os.path.dirname(os.path.abspath(this_file))

def find_dev_root(d):
     setup_py = os.path.join(d, 'setup.py')
     if os.path.exists(setup_py):
        return d
     parent = os.path.dirname(d)
     if parent == d:
        return None
     return find_dev_root(parent)

dev_root = find_dev_root(bin_dir())
if dev_root is None:
     msg = "Could not find development environment root"
     print >> sys.stderr, msg

Is there an easier way, say, os.path.find_in_ancestors('setup.py') or
something similar.

- Jeff Younker - jeff at drinktomi.com -

More information about the Baypiggies mailing list