Basic importing question

Hussein B hubaghdadi at gmail.com
Wed Aug 20 06:53:33 EDT 2008


On Aug 20, 5:43 am, John Machin <sjmac... at lexicon.net> wrote:
> 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

Thank you both for your kind help and patience :)
Built-in modules are compiled but even if they are so, when importing
them (sys for example), Python will run their code in order to create
bindings and objects, right?
I'm learning Python and I want to learn it well, so that I'm asking a
lot :)



More information about the Python-list mailing list