[Tutor] Importing sub modules

Steven D'Aprano steve at pearwood.info
Fri Apr 1 00:37:52 CEST 2011


Prasad, Ramit wrote:
>>> In any event, you shouldn't be worrying about something like overhead until after your base prorgram is written.
> 
> Base programs are written. I was just looking into insight about the mechanics behind importing :)
> For instance, do these libraries normally lazy load? If you have mod1.mod2 and mod1.mod3 and import mod1 does it usually also internally load mod2/3? import mod1.mod2 (or do from mod1 import mod2) does it avoid any loading of mod3 or does it do that as part of loading mod1?

That depends on the module.

In the case of os, it has code like the following:


if 'posix' in _names:
     name = 'posix'
     linesep = '\n'
     from posix import *
     try:
         from posix import _exit
     except ImportError:
         pass
     import posixpath as path

     import posix
     __all__.extend(_get_exports_list(posix))
     del posix

elif 'nt' in _names:
     # ... similar code
elif 'os2' in _names:
     # ...
elif 'mac' in _names:
     # ...
elif 'ce' in _names:
     # ...
elif 'riscos' in _names:
     # ...
else:
     # raise exception

So in the case of os, no, it does not lazily load path only when needed, 
and os.path is available as soon as you import os, without any further 
imports.

However, if the module is a more modern package, the situation *may* be 
different. A package uses a directory with sub-modules, plus an 
__init__.py file. In this case, the behaviour is entirely up to the author.

Here's an example from one of my own Python 3 libraries: I have a 
package that (currently) looks something like this:

stats/
   +--  __init__.py
   +--  co.py
   +--  order.py
   +--  utils.py
   +--  _tests/
          +--  test_co.py
          ... etc.

(greatly simplified).

When you call "import stats", Python reads the file stats/__init__.py, 
compiles it and creates a module from it. __init__.py in turn includes a 
  line "from . import utils". (This is Python 3 syntax, so it doesn't 
work with Python 2.) That line imports the stats/utils.py submodule and 
makes it available from within stats as stats.utils.

However the sub-modules co.py and order.py are *not* imported by the 
main module. They are only loaded on demand, when you say "import 
stats.co" or "from stats.order import median" or similar.

So it depends on the module.



-- 
Steven



More information about the Tutor mailing list