"weird" behavior of __file__ attribute

Jinu Lee jinu_lee at hotmail.com
Tue Sep 11 22:36:25 EDT 2001


The result is not related to the way you call mytest.py.
The first time you call it, mytest.py imports mytestA.py and python
interpreter creates compiles mytestA.py into mytestA.pyc.
After that, when you call mytest.py again and it imports mytestA.py, the
interpreter finds mytetsA.pyc and imports that file.

If you delete the mytestA.pyc file and call mytest.py in your second way,
you will get the same result as the first one.

Jinu



"cherzog" <ccf.herzog at gmx.net> wrote in message
news:9naf7h$ojk$1 at news.urz.uni-heidelberg.de...
> Hello All,
> The __file__ attribute of a module behaves in a way I do not understand:
> I have the following example on Windows with Python 2.1: I got two files
in
> C:\
> first: C:\mytest.py
> second: C:\mytestA.py
>
> mytest.py is:
> #----------start of file------
> import os
> import mytestA
> print "cwd: ", os.getcwd()
> print "mytestA.__file__: ", mytestA.__file__
> #----------end of file
>
> while mytestA.py says simply hello:
>
> #----------start of file------
> print "hello"
> #----------end of file
>
> If I type:
> C:\>mytest.py
> I get:
> Hallo
> cwd:  C:\
> mytestA.__file__:  C:\mytestA.py
>
> But if I type:
> C:\>C:\programme\python21\python mytest.py
> I get:
> Hallo
> cwd:  C:\
> mytestA.__file__:  mytestA.pyc
>
> This is not what I wanted because I hoped to find out the exact path of
> module mytestA.py no matter  how I call mytest.py.
> Please, can someone help me?
> Thank you
> Christoph
>
>





More information about the Python-list mailing list