py vs pyc

mrmakent at mrmakent at
Mon Jan 9 15:34:31 CET 2006

Sakcee wrote:

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

When you run 'python', the python interpreter first looks to
see if 'test.pyc' (which is the byte-code compiled version of
'') exists, and if it is more recent than ''.  If so, the
interpreter runs it.  If it does not exist, or '' is more recent
than it (meaning you have changed the source file), the interpreter
first compiles '' 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 '' 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 '', make it executable, and then
run '' by itself (without explicitly invoking 'python', then
'' does not get compiled to 'test.pyc', meaning that the
compilation step has to be done every time you run '' this way.

More information about the Python-list mailing list