Getting "current" module file ?

Emile van Sebille emile at
Sat Feb 10 14:53:09 EST 2001

For imported modules, a __file__ attribute exists that tells
where it came from.

If the name of the current module is __main__, you'll need
to check argv for the file name, but unless it's fully
qualified, you'll need to guess at the directory.  pwd may
help, but if it has been changed, may not.
import bisect
print bisect.__file__ # --> d:\python20\lib\bisect.pyc
print __name__ # --> __main__
print __main__.__file__ # --> error

Traceback (most recent call last):
  File "C:\My Documents\", line 4, in ?
    print __main__.__file__
NameError: There is no variable named '__main__'


Emile van Sebille
emile at

"Olivier DECKMYN" <olivier.deckmyn at> wrote in
message news:964317$1hpm$1 at
> Hi all,
> I am looking for a magic function that would tell me where
is stored the
> file that is currently running.
> A good example is better that thousands of words :
> here is /home/olivier/python/ :
> """
> import string
> ...
> print "The current module test1 is stored in",
> """
> would get :
> The current module test1 is stored in
> Thanx for your support.
> ps : Python is real nice piece of code...

More information about the Python-list mailing list