Linux application deployment

Alex Martelli aleaxit at yahoo.com
Mon Sep 6 09:24:05 CEST 2004


Robert M. Emmons <RobMEmmons at cs.com> wrote:
   ...
> One thing I've always wonders was -- Is there a way for a python module
> to tell where it's file is located???  If you could do this it might 

Piece of cake: the key part of this recipe is
    def whereami(): return sys.modules[__name__].__file__
then you can os.path.abspath this filename string as you wish.

Here's a more complete example foo.py:

#!/usr/bin/env python

import sys, os

def whereami(): return os.path.abspath(sys.modules[__name__].__file__)

if __name__ == '__main__': print whereami()


Put this anywhere on your $PATH, chmod +x it, and try out what you see
by executing it as the main script from various locations... should work
pretty reliably.

> also be possible to have a python script reset sys.path based on it's
> own location.

Sure, that shouldn't be impossible.


Alex



More information about the Python-list mailing list