why have to "from compiler import *"

Paddy paddy3118 at netscape.net
Tue Sep 5 06:59:06 EDT 2006


mark_galeck_spam_magnet at yahoo.com wrote:
> Hi,  why does
>
> >>> import compiler
> >>> compileFile("foo.py")
>
> complain name 'compileFile' not defined.  But
>
> >>>from compiler import *
>
> works.  Why?  (I did read the tutorial, it seems to say "import module"
> should work.
>
> Thank you,  Mark

I did some ascii art to show what happenss to your namespace as you
import from a module in a few ways. (You'll need to view this in a
NON-proportional font).

You say you've read the docs at: http://docs.python.org/tut/node8.html
or http://docs.python.org/ref/import.html

Heres my 'art':



 module1.py:
    O-----------------O
    |moduleFunc1      |
    |moduleClass1     |
    |  class1Method1  |
    |  class1Method2  |
    |moduleFunc2      |
    |moduleVar1       |
    |moduleVar2       |
    O-----------------O

 # Empty namespace:
   \-----------------/
   |                |
   |                |
   |                |
   |                |
   |                |
   |                |
   |                |
   /-----------------\


 import module1
 # namespace becomes:
   \-------------------------/
   |module1.moduleFunc1     |
   |module1.moduleClass1:   |
   |  class1Method1         |
   |  class1Method2         |
   |module1.moduleFunc2     |
   |module1.moduleVar1      |
   |module1.moduleVar2      |
   /------------------------\

 from module1 import *
 # namespace becomes:
   \-----------------/
   |moduleFunc1      |
   |moduleClass1     |
   |  class1Method1  |
   |  class1Method2  |
   |moduleFunc2      |
   |moduleVar1       |
   |moduleVar2       |
   /-----------------\

 from module1 import moduleFunc1, muduleVar2
 # namespace becomes:
   \-----------------/
   |moduleFunc1      |
   |moduleVar2       |
   |                 |
   /-----------------\

 from module1 import moduleClass1 as C1
 # namespace becomes
   \-----------------/
   |C1               |    # C1 === module1.moduleClass1
   |                 |
   /-----------------\



- Paddy.
P.S. I'm playing with http://www.jave.de
       The Java Ascii Versatile Editor.




More information about the Python-list mailing list