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

Daniel P. M. Silva dsilva at
Tue Nov 4 05:56:30 CET 2003

On Mon, 03 Nov 2003 18:34:33 -0500, Ellinghaus, Lance wrote:
> If I remember correctly, Yes. 
> Python will import each module, compile if necessary, and then execute it.
> This might cause additional modules to be compiled, loaded, etc.. But Python
> will not import a module more than one time. Each additional time the import
> is called for the same module, Python just binds a new reference into the
> namespace.

What about this?

[/tmp] > cat >
import b
print "module A loaded"
[/tmp] > cat >
import a
print "module B loaded"
[/tmp] > python
module A loaded
module B loaded
module A loaded

> -----Original Message-----
> From: seberino at [mailto:seberino at] 
> Sent: Monday, November 03, 2003 3:28 PM
> To: Ellinghaus, Lance
> Cc: python-list at
> Subject: Re: Does python always need to compile ENTIRE program before it c
> an start to run it???
> Lance
> Thanks for the info.  I agree that the initial script you
> run must be compiled completely before it can execute.
> Does "completely" =  all the imported modules too?
>> 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 
>> (.py) then it will load the saved compilation and then run it. If the 
>> file is not 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.
> The first word of your comments above is "When".  So if Python doesn't load
> a module until 3 hours into the execution of a large Python program then
> your recipe above (including compilation of module) won't get run until 3
> hours AFTER execution starts right?
> Chris

More information about the Python-list mailing list