Reputation: 77
While going through many concepts, this comes to my min (it may be silly). Please advise me how following scenario being achieved
Say in a folder I have "z.pyc" file (no z.py).
Now I created z.py at the same location.
As per my understanding first time execution of z.py will update z.pyc , so from second time trial z.py and z.pyc should give same result.
But it is not happening. z.py and z.pyc are giving distinguish results every time. Please suggest how this scenario being achieved.
Upvotes: 0
Views: 38
Reputation: 168706
"first time execution of z.py will update z.pyc"
No, "execution of z.py
" will never create, update, nor reference, z.py
. Only an import z
statement will create or make use of z.pyc
.
1) .pyc
files are created when one performs an import
of a file, never when run executes a file.
2) import z
will refresh the compiled version, if required.
3) Running python z.pyc
will always run the compiled version of the file, whereas python z.py
will never run the compiled version of the file.
So, one sequence of events that might lead to the confusing results above is:
$ echo "print 'first file'" > z.py
$ echo "import z" > main.py
$ python main.py
first file
$ echo "print 'second file'" > z.py
$ python z.py
second file
$ python z.pyc
first file
The lesson to learn? Never run python z.pyc
, always run python z.py
.
Upvotes: 2