Basic importing question

John Machin sjmachin at lexicon.net
Wed Aug 20 06:43:08 EDT 2008


On Aug 20, 8:08 pm, Hussein B <hubaghd... at gmail.com> wrote:
> 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?
> Thanks.

sys is a built-in module, so the answer is zero times.

For a non-builtin module foo where there exists:
(1) only a foo.py, it will be compiled into foo.pyc
(2) only a foo.pyc, it will be used
(3) both a foo.py and a foo.pyc, Python compiles the foo.py if the pyc
is out of date or (so I believe [*]) was created by a different
version of Python.

Subsequent imports will use the in-memory copy (in sys.modules, IIRC
[*]) ...

[*] == Please save me the bother of checking this in the manual :-)

HTH,
John



More information about the Python-list mailing list