why have to "from compiler import *"

Bruno Desthuilliers onurb at xiludom.gro
Tue Sep 5 04:15:15 EDT 2006


mark_galeck_spam_magnet at yahoo.com wrote:
> Hi,  why does
> 
>>>> import compiler
>>>> compileFile("foo.py")
> 
> complain name 'compileFile' not defined.  

Probably because it's not ?

import <modulename> imports the name <modulename> in the current
namespace. Then <modulename> let you access all the names defined in
<modulename> namespace. So in you're case, it should be:

>>>> import compiler
>>>> compiler.compileFile("foo.py")


> But
> 
>>> >from compiler import *
> 
> works. 

"from <modulename> import <somename>" directly loads <somename> into the
current namespace. The 'import *' loads all public names from
<modulename>.  And FWIW it's usually considered bad style (potential
name clash, and can makes hard to tell where a name is defined...)

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list