Module directory

Alex Martelli aleaxit at yahoo.com
Fri Sep 8 05:28:43 EDT 2000


"Gilles Lenfant" <glenfant at nospam-e-pack.net> wrote in message
news:8p8l43$piv$1 at reader1.imaginet.fr...
> Hi,
>
> I'm looking for a simple (and portable) way to get the path of the module
> from within a function it contains.

A module has a __file__ attribute that gives the complete path
to the file from which the module has been loaded.  Therefore:

    os.path.dirname(__file__)

when called from any function (with an import os.path in the
module, of course:-) will give the directory-name from which
the module that contains the function was loaded (this assumes
the function's func_globals have not been tampered with, of
course, but who ever does that?).


Alex






More information about the Python-list mailing list