py vs pyc

mrmakent at cox.net mrmakent at cox.net
Mon Jan 9 09:34:31 EST 2006


Sakcee wrote:

> what is the difference between runing from cmd  "python test.py" and
> "python test.pyc"

When you run 'python test.py', the python interpreter first looks to
see if 'test.pyc' (which is the byte-code compiled version of
'test.py') exists, and if it is more recent than 'test.py'.  If so, the
interpreter runs it.  If it does not exist, or 'test.py' is more recent
than it (meaning you have changed the source file), the interpreter
first compiles 'test.py' to 'test.pyc'.

So you can see that you don't need to directly run 'test.pyc'; the
interpreter handles all that for you.  Except for the very first time
you run 'test.py' after creating or updating it, python will
automactically run the pre-compiled version, the '.pyc'.

There is one exception to all the above: If you put '#! /usr/bin/env
python' on the first line of 'test.py', make it executable, and then
run 'test.py' by itself (without explicitly invoking 'python', then
'test.py' does not get compiled to 'test.pyc', meaning that the
compilation step has to be done every time you run 'test.py' this way.




More information about the Python-list mailing list