By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,650 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,650 IT Pros & Developers. It's quick & easy.

why have to "from compiler import *"

P: n/a
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

Sep 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mon, 2006-09-04 at 21:40 -0700, ma*********************@yahoo.com
wrote:
Hi, why does
>import compiler
compileFile("foo.py")

complain name 'compileFile' not defined. But
Because 'compileFile' is not defined. But I think you'll find that
compiler.compileFile IS defined.

John Purser

>
>>from compiler import *

works. Why? (I did read the tutorial, it seems to say "import module"
should work.

Thank you, Mark
Sep 5 '06 #2

P: n/a
ma*********************@yahoo.com wrote:
Hi, why does
>>>import compiler
compileFile("foo.py")

complain name 'compileFile' not defined.
Probably because it's not ?

import <modulenameimports the name <modulenamein the current
namespace. Then <modulenamelet you access all the names defined in
<modulenamenamespace. So in you're case, it should be:
>>>import compiler
compiler.compileFile("foo.py")
But
>>from compiler import *

works.
"from <modulenameimport <somename>" directly loads <somenameinto 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 'o****@xiludom.gro'.split('@')])"
Sep 5 '06 #3

P: n/a
ma*********************@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.

Sep 5 '06 #4

P: n/a
"Paddy" <pa*******@netscape.netwrote:
>
import module1
# namespace becomes:
\-------------------------/
|module1.moduleFunc1 |
|module1.moduleClass1: |
| class1Method1 |
| class1Method2 |
|module1.moduleFunc2 |
|module1.moduleVar1 |
|module1.moduleVar2 |
/------------------------\
It would be more accurate to say:
# namespace becomes
\-----------------/
|module1 |
| |
/-----------------\
Sep 5 '06 #5

P: n/a
Duncan Booth wrote:
"Paddy" <pa*******@netscape.netwrote:

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

It would be more accurate to say:
# namespace becomes
\-----------------/
|module1 |
| |
/-----------------\
Possibly, although I purposely wanted to give a flavour of how things
might be accessed.

- Paddy

Sep 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.