Compiled bytecode

Peter Hansen peter at engcorp.com
Thu Dec 30 23:03:17 CET 2004


Rocco Moretti wrote:
> Peter Hansen wrote:
>> The main script is generally not compiled, but all imported
>> scripts are generally compiled automatically, the first time
>> they are imported, and never again unless the source changes.
> 
> Someone please correct me if I'm wrong, but I'm under the impression 
> that the main script *is* compiled, but the byte-code compiled file is 
> kept in memory, and not written to disk.

Rocco, you're quite correct and I misspoke.  I did of course
mean "the main script's bytecode is not written to disk in
a .pyc file".

To the OP: one might interpret your question differently, now
that I look again at your usage of "compiled".  If you
thought that code that didn't get into a .pyc file was somehow
not compiled, you were wrong.  *All* Python code gets compiled,
whether it's in the main script, imported modules, or even
in an exec statement or a call to eval().  The only difference
is that imported modules have the output of the compilation
process (i.e. the bytecode) cached in a .pyc or .pyo file
to allow skipping a later redundant recompilation stage if
the .py source has not changed.  Therefore one could say that
Python never executes .py files, so your observation about
performance is correct: there is no difference.

-Peter



More information about the Python-list mailing list