Basic importing question

Hussein B hubaghdadi at gmail.com
Wed Aug 20 07:30:26 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

One more question:
If I have this structure:
orig/com/domain/project/Klass1.py
                        Klass2.py
                        __init__.py

Why com, domain, project should have __init__.py also? they don't
contain source code files?
Thanks again.



More information about the Python-list mailing list