Basic importing question

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Aug 20 06:42:10 EDT 2008


Hussein B a écrit :
> Hey,
> Suppose I have a Python application consists of many modules (lets say
> it is a Django application).
> If all the modules files are importing sys module, how many times the
> sys module will be compiled and executed?
> Only once (the first time the PVM locates, compiles and executes the
> sys module)? or once for each module importing sys?

Hmmm.... Well, could it be possible that Python's developper were smart 
enough to implement some cache mechanism here ?-)

import do (approximately) the following:

check if the module already exists in sys.modules
if not:
   locate the module's source in sys.path
   if not found
     raise an import error
   look for a compiled .py file
   if there's none, or if there's one and it's older than the .py file:
     compile the source and save it as .pyc
   execute the .pyc file and build a module object from it
   add the module object to sys.modules
bind the relevant names into your current namespace



More information about the Python-list mailing list