[Tutor] Module imports

Kent Johnson kent37 at tds.net
Mon Jul 16 04:13:37 CEST 2007

kevin parks wrote:

> 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

It is actually required to import a module everywhere you want to use
it. Noufal and Dave have explained that the actual loading of the module
only happens once. Another thing import does is to introduce the name of
the imported module into the namespace of the importing module. In order
to be able to refer, for example, to sys.maxint, the sys module must be
bound to the name 'sys'. That binding is done by the import statement.
So if you didn't import sys in a module that used it you would get
NameErrors when you tried to access attributes of the sys module.


More information about the Tutor mailing list