473,413 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

why have to "from compiler import *"

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
5 2153
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gerald.maher | last post by:
Hi, I am trying to excuate the follwong code: I get the following error: Here is my Lib path:
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
1
by: Xiao Jianfeng | last post by:
Hello, In pymol I can use "from chempy import Atom" but "import chempy.Atom" doesn't work. It says,"ImportError: No module named Atom". What is going wrong ? Thanks
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
2
by: mithrond | last post by:
i can't use "from __future__ import ..." statement import two statesments in the same time. for the simple code: from __future__ import with_statement, division with file('url.txt','r') as f:...
1
by: lemke_juergen | last post by:
Hi everyone, I define some vars and functions in a "support" module which gets called from my main app module. Using Python 2.5. I import all symbols in the support module at the top of the...
1
by: Malcolm Greene | last post by:
Is there any consensus on what "from __future__ import" options developers should be using in their Python 2.5.2 applications? Is there a consolidated list of "from __future__ import" options to...
1
by: Eric Hanchrow | last post by:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.) This seems so basic that I'm surprised that I didn't find anything about it in the FAQ. (Yes, I am fairly new to Python.) Here are...
3
by: J. Cliff Dyer | last post by:
On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote: It's the same reason as this: 5 5 6 5 Python "variables" are just names that point at objects. When you
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.