[Tutor] module imports

Kent Johnson kent37 at tds.net
Thu Mar 9 02:55:46 CET 2006


kevin parks wrote:
> so let's say i have a script called foo.py.
> foo.py uses some things from the random module and therefore has
> 
> import random
> import kp
> import sys
> import time
> 
> etc. & co.  in it
> 
> but foo.py also imports a module kp which also
> happens to import random.
> 
> Should i only be importing random once or are you saying it
> is just fine to import a module that imports another module that
> you actually have already imported.

It's fine. Each module that needs to use random (for example) should 
import it. Only the first import to be executed has any appreciable 
cost; the subsequent ones just put a reference to the already-loaded and 
cached module into the current module namespace.

Kent



More information about the Tutor mailing list