[Tutor] Module imports

Dave Kuhlman dkuhlman at rexx.com
Wed Jul 11 17:34:35 CEST 2007

On Wed, Jul 11, 2007 at 08:24:17PM +0900, kevin parks wrote:
> but my kp module also uses happens to call on certain things from the
> random, time, and sys modules and so kp.py also has
> import random
> import sys
> import time
> Now so far this seems to be working fine and without error (as far as  
> i can tell). However, shouldn't i only be importing random, sys and  
> time once? and if so, where? in foo.py or kp.py?

No need to worry.  You *are* only importing each module once. 
Python checks, and only imports a module if it has not already been
imported.  Here is a quote from the section on the import statement
in the Python language reference.

    "The system maintains a table of modules that have been or are being
    initialized, indexed by module name. This table is accessible as
    sys.modules. When a module name is found in this table, step (1) is
    finished. If not, a search for a module definition is started. When
    a module is found, it is loaded. Details of the module searching
    and loading process are implementation and platform specific. It
    generally involves searching for a `built-in'' module with the
    given name and then searching a list of locations given as
            -- http://docs.python.org/ref/import.html

You can prove this to yourself by writing a tiny module with a
print statement at top level (outside any function or class
definition), then importing it twice.  The print statement should
only write out its message one time.

This has consequences, by the way.  Calculations that performed at
a top level in a module are performed only once, for example.

> It was explained to me that it is fine to import random, sys and time  
> in both, and that only the first import uses up memory, and  
> subsequent attempts to import the same module don't really cost  
> anything and just add a reference in the namespace. but isn't loading  
> it in both modules confusing and bad .... additionally in this case  
> which import is actually being used (or does this not even matter?)  
> the one in kp.py or in foo.py?

No. It's not bad.  It's considered good practice.  And, considered
the opposite.  If we were required to only import each module once,
then before I added an import statement to my code, I'd have to
check every other module I import and every module imported by
those modules etc. for duplicates.

> For some reason i feel like i should understand how and why this  
> works a little better in order to avoid overlap and conflict in what  
> is becoming a bit more involved intermingling of modules and scripts.
> or alternately you all can chime in and say "dude, get over it,  
> multiple and overlapping imports are not a big deal in python, you  
> are worrying about nothing,  and making a problem where there is  
> none! Get on with your life." haha

It is not that you should "try not to worry".  It's that you
are doing things the way you are intended to.  And, you are
probably in sync with the cosmos, too.


Dave Kuhlman

More information about the Tutor mailing list