Does python always need to compile ENTIRE program before it c an start to run it???

Ellinghaus, Lance lance.ellinghaus at
Tue Nov 4 00:34:33 CET 2003

Hello Francis,

Actually we are both wrong. I just looked at the code (should have
remembered this.. I did a lot of work on this MANY moons ago, but that is
another story)...

When a .pyc or .pyo file is written, the beginning of the file contains the
MAGIC number, that validates the BYTECODE version, and the mtime
(Modification Time) of the .py file.
If the MAGIC number does not match the one needed, it recompiles the .py
file and writes out a new one.
If the stored mtime does not match the mtime of the original .py file, it
recompiles the .py file and writes out a new one.

The process does not look at the mtime of the .pyc/.pyo file.

Sorry for the confusion.

Lance Ellinghaus
EDS - TWAI Operations
Phone: 214-922-5454
Cell: 940-597-4755

-----Original Message-----
From: Francis Avila [mailto:francisgavila at] 
Sent: Monday, November 03, 2003 3:15 PM
To: python-list at
Subject: Re: Does python always need to compile ENTIRE program before it can
start to run it???

"Ellinghaus, Lance" <lance.ellinghaus at> wrote in message
news:mailman.385.1067888457.702.python-list at
> Python always compiles your scripts to bytecode before it can be 
> executed. It cannot and does not do partial compilation and execution.
> But this is not the whole story. Python does not always have to do the 
> compilation phase. When Python loads a module (.py file) from an 
> 'import' statement, it will see if there is an already compiled 
> version in the same directory and named the same except for the '.pyc' 
> or '.pyo' extension. If it finds this file AND it is newer than the 
> corresponding source code
> then it will load the saved compilation and then run it. If the file 
> is
> found or it is older than the source, then Python will compile the 
> source, attempt to save the compiled version, and then it will execute 
> it.

Actually, I Python will only use a compiled version if the source and the
compiled version have *exactly* the same date.  (This is a much better
approach anyway--suppose you want to run an older version?)

I only say this because I read "if it is newer" more often than not.

This reference has the rundown on the relationship between py-pyc-pyo files:

Francis Avila


More information about the Python-list mailing list