Question about import and namespace

Peter Otten __peter__ at web.de
Fri Sep 1 09:34:27 EDT 2006


jdemoor at gmail.com wrote:

> Thanks for the replies.
> 
>> You can do both
>>
>> from module import *
>> import module
>>
>> as these kinds of import are not mutually exclusive.
> 
> Would this run the code in 'module' twice, or just make the objects in
> it accessible by several names ?

The latter. But why don't you try it yourself by putting a 

print "importing module" # you will see that once

statement in the module? 

There is one notable exception, the application's main module:

$ cat main.py
import main
print "importing main as", __name__
$ python main.py
importing main as main
importing main as __main__

Peter



More information about the Python-list mailing list