[Baypiggies] Locating the directory you are executing from

Adam Hupp adam at hupp.org
Sun May 4 21:05:40 CEST 2008


I'm not aware of any standard library call that does this, but you can replace:

sys.modules[__name__].__file__

with just "__file__".

On Sun, May 4, 2008 at 11:37 AM, Jeff Younker <jeff at drinktomi.com> wrote:
> 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
>  overlooked.
>
>
>  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 have
>  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
>     sys.exit(1)
>  os.chdir(dev_root)
>
>
>  Is there an easier way, say, os.path.find_in_ancestors('setup.py') or
>  something similar.
>
>  - Jeff Younker - jeff at drinktomi.com -
>
>
>
>  _______________________________________________
>  Baypiggies mailing list
>  Baypiggies at python.org
>  To change your subscription options or unsubscribe:
>  http://mail.python.org/mailman/listinfo/baypiggies
>



-- 
Adam Hupp | http://hupp.org/adam/


More information about the Baypiggies mailing list