Getting "current" module file ?

Emile van Sebille emile at fenx.com
Sat Feb 10 20:53:09 CET 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.

#-----test.py----
import bisect
print bisect.__file__ # --> d:\python20\lib\bisect.pyc
print __name__ # --> __main__
print __main__.__file__ # --> error
#----end test.py----


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

--

Emile van Sebille
emile at fenx.com
-------------------


"Olivier DECKMYN" <olivier.deckmyn at mail.dotcom.fr> wrote in
message news:964317$1hpm$1 at feed.teaser.fr...
> 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/test1.py :
> """
> import string
> ...
>
> print "The current module test1 is stored in",
magic_get_location()
> """
>
> would get :
> The current module test1 is stored in
/home/olivier/python/test1.py
>
> Thanx for your support.
>
> ps : Python is real nice piece of code...
>
>
>
>
>





More information about the Python-list mailing list