How to obtain python file path

Geoff Gerrietts geoff at gerrietts.net
Tue Apr 1 17:12:52 EST 2003


Dan Grassi wrote:
>Not quite, that only provides the path to the initial .py file.
>
>With a directory structure:
>fa
>    a.py
>    fb
>        b.py
>
>cd .../fa
>execute a.py
>where a adds fb to sys.path
>a imports b.py
>
>I need b.py to report it's path which would be .../fa/fb

If you import fb, fb.__path__[0] will be a relative path to the
directory that produced fb.

If you import fb.b, fb.b.__file__ will be the name of the file that
was loaded to create module b (usu. a *.pyc, *.py, or *.pyo file).

You can use the sys.argv[0] and os.path tricks to find your answers
from there, I think?

Luck,
--G.

-- 
Geoff Gerrietts             "Punctuality is the virtue of the bored." 
<geoff at gerrietts net>                               --Evelyn Waugh





More information about the Python-list mailing list