473,545 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple import question about mac osx

Hi, I have this code (learning from Core Python, Chun's book), module
named chap2.py.

class FooClass(object ):
version=0.1

def __init__(self, nm='John Doe'):
self.name=nm
print 'Created a class instance for ', nm
def showname(self):
print 'Your name is', self.name
print 'My name is', self.__class__. __name__

On Windows, if I compile this and then in the python interpreter type:
>>import chap2
foo1=FooClass ()
Created a class instance for John Doe
>>>
If I do the same think on my Mac OS X 10.5.2

NameError: name 'FooClass' is not defined.

I thought it was the path and did export PATH=$PATH:/mypath/
topythoncode

but it did not help.

What am I doing wrong? Thank you.
Jun 27 '08 #1
8 1112
On Apr 29, 1:16*pm, jmDesktop <needin4mat...@ gmail.comwrote:
Hi, I have this code (learning from Core Python, Chun's book), module
named chap2.py.

class FooClass(object ):
* * * * version=0.1

* * * * def __init__(self, nm='John Doe'):
* * * * * * * * self.name=nm
* * * * * * * * print 'Created a class instance for ', nm
* * * * def showname(self):
* * * * * * * * print 'Your name is', self.name
* * * * * * * * print 'My name is', self.__class__. __name__

On Windows, if I compile this and then in the python interpreter type:
>import chap2
foo1=FooClass( )

Created a class instance for *John Doe

If I do the same think on my Mac OS X 10.5.2

NameError: name 'FooClass' is not defined.

I thought it was the path and did export PATH=$PATH:/mypath/
topythoncode

but it did not help.

What am I doing wrong? *Thank you.
forgot to say that on the mac I can do import chap2, but when I try
and instantiate I get the error above.
Jun 27 '08 #2
On Apr 29, 12:46 pm, jmDesktop <needin4mat...@ gmail.comwrote:
On Apr 29, 1:16 pm, jmDesktop <needin4mat...@ gmail.comwrote:
Hi, I have this code (learning from Core Python, Chun's book), module
named chap2.py.
class FooClass(object ):
version=0.1
def __init__(self, nm='John Doe'):
self.name=nm
print 'Created a class instance for ', nm
def showname(self):
print 'Your name is', self.name
print 'My name is', self.__class__. __name__
On Windows, if I compile this and then in the python interpreter type:
>>import chap2
>>foo1=FooClass ()
Created a class instance for John Doe
If I do the same think on my Mac OS X 10.5.2
NameError: name 'FooClass' is not defined.
I thought it was the path and did export PATH=$PATH:/mypath/
topythoncode
but it did not help.
What am I doing wrong? Thank you.

forgot to say that on the mac I can do import chap2, but when I try
and instantiate I get the error above.
It shouldn't work under Windows, either. You have to qualify the name
of the class with the name of the module, as in chap2.FooClass( ). Or
you can type "from chap2 import FooClass" and then you'll be able to
simply say FooClass().
Jun 27 '08 #3
On Apr 29, 1:54*pm, s0s...@gmail.co m wrote:
On Apr 29, 12:46 pm, jmDesktop <needin4mat...@ gmail.comwrote:


On Apr 29, 1:16 pm, jmDesktop <needin4mat...@ gmail.comwrote:
Hi, I have this code (learning from Core Python, Chun's book), module
named chap2.py.
class FooClass(object ):
* * * * version=0.1
* * * * def __init__(self, nm='John Doe'):
* * * * * * * * self.name=nm
* * * * * * * * print 'Created a class instance for ',nm
* * * * def showname(self):
* * * * * * * * print 'Your name is', self.name
* * * * * * * * print 'My name is', self.__class__. __name__
On Windows, if I compile this and then in the python interpreter type:
>import chap2
>foo1=FooClass( )
Created a class instance for *John Doe
If I do the same think on my Mac OS X 10.5.2
NameError: name 'FooClass' is not defined.
I thought it was the path and did export PATH=$PATH:/mypath/
topythoncode
but it did not help.
What am I doing wrong? *Thank you.
forgot to say that on the mac I can do import chap2, but when I try
and instantiate I get the error above.

It shouldn't work under Windows, either. You have to qualify the name
of the class with the name of the module, as in chap2.FooClass( ). Or
you can type "from chap2 import FooClass" and then you'll be able to
simply say FooClass().- Hide quoted text -

- Show quoted text -
Thanks. That worked on mac. But it does work like I said in
Windows. Don't know why. Mr. Chun must also be using Windows because
that is the way he does it in his book.
Jun 27 '08 #4
On Tue, Apr 29, 2008 at 2:14 PM, jmDesktop <ne***********@ gmail.comwrote:
Thanks. That worked on mac. But it does work like I said in
Windows. Don't know why. Mr. Chun must also be using Windows because
that is the way he does it in his book.
It shouldn't work that way on windows either. Can you tell us a
little more about what you mean when you say you "compile this" under
windows? Normally, python code doesn't need to be compiled, so I'm
wondering if you're doing something different from what we expect when
you say you compile it and then import it in the interpreter.

Are you by any chance writing code in IDLE, running it, then doing
things in the interpreter?

--
Jerry
Jun 27 '08 #5
On Apr 29, 2:37*pm, "Jerry Hill" <malaclyp...@gm ail.comwrote:
On Tue, Apr 29, 2008 at 2:14 PM, jmDesktop <needin4mat...@ gmail.comwrote:
*Thanks. *That worked on mac. *But it does work like I said in
*Windows. *Don't know why. *Mr. Chun must also be using Windows because
*that is the way he does it in his book.

It shouldn't work that way on windows either. *Can you tell us a
little more about what you mean when you say you "compile this" under
windows? *Normally, python code doesn't need to be compiled, so I'm
wondering if you're doing something different from what we expect when
you say you compile it and then import it in the interpreter.

Are you by any chance writing code in IDLE, running it, then doing
things in the interpreter?

*--
Jerry
On Windows I took the text file I created on mac with vi and opened it
in PythonWin. I ran it. It compiled. I run the import and call from
the python interpreter.
Jun 27 '08 #6
On Apr 29, 2:17 pm, jmDesktop <needin4mat...@ gmail.comwrote:
On Apr 29, 2:37 pm, "Jerry Hill" <malaclyp...@gm ail.comwrote:
On Tue, Apr 29, 2008 at 2:14 PM, jmDesktop <needin4mat...@ gmail.comwrote:
Thanks. That worked on mac. But it does work like I said in
Windows. Don't know why. Mr. Chun must also be using Windows because
that is the way he does it in his book.
It shouldn't work that way on windows either. Can you tell us a
little more about what you mean when you say you "compile this" under
windows? Normally, python code doesn't need to be compiled, so I'm
wondering if you're doing something different from what we expect when
you say you compile it and then import it in the interpreter.
Are you by any chance writing code in IDLE, running it, then doing
things in the interpreter?
--
Jerry

On Windows I took the text file I created on mac with vi and opened it
in PythonWin. I ran it. It compiled. I run the import and call from
the python interpreter.
Well, Python compiles automatically any .py file that you import. Of
course this isn't machine code like the one from a C compiled
executable. It's Python's own bytecode. But normally you shouldn't
worry about that bytecode; actually, you shouldn't even be paying
attention to it. All that concerns you is that you created a module
and that you can import it. Leave the whole "compiling" concept to the
interpreter.
Jun 27 '08 #7
On Tue, Apr 29, 2008 at 3:17 PM, jmDesktop <ne***********@ gmail.comwrote:
On Windows I took the text file I created on mac with vi and opened it
in PythonWin. I ran it. It compiled. I run the import and call from
the python interpreter.
You're not doing what you think you're doing. I'm not sure I know the
right way to explain it, though.

When you run your code in pythonwin, it's just like calling 'python -i
chap2.py' It runs the code in chap2.py, then gives you an interpreter
window to interact with your code. In this case, that means that
FooClass is visible with no import at all, because it was defined in
the scope of the currently running script, as opposed to being
imported.

You haven't said exactly how you're doing this on your mac, but I'm
guessing that you're opening a command line, starting up the python
interpreter, then going from there?

Can someone help me out? I'm running into a mental block on how to
explain the difference between doing this:
C:\Python25>pyt hon -i chap2.py
>>foo1=FooClass ()
Created a class instance for John Doe
>>>
and doing this:
C:\Python25>pyt hon
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
>>import chap2
foo1=chap2.Fo oClass()
Created a class instance for John Doe
>>>
--
Jerry
Jun 27 '08 #8
On Apr 29, 3:47*pm, "Jerry Hill" <malaclyp...@gm ail.comwrote:
When you run your code in pythonwin, it's just like calling 'python -i
chap2.py' *It runs the code in chap2.py, then gives you an interpreter
window to interact with your code. *In this case, that means that
FooClass is visible with no import at all, because it was defined in
the scope of the currently running script, as opposed to being
imported.

You haven't said exactly how you're doing this on your mac, but I'm
guessing that you're opening a command line, starting up the python
interpreter, then going from there?

Can someone help me out? *I'm running into a mental block on how to
explain the difference between doing this:
C:\Python25>pyt hon -i chap2.py>>foo1= FooClass()
jmDesktop,

With what Jerry stated,

You can see what is happening under PythonWin interactive window by
doing:
>>dir()
before and after running chap2.py and see that FooClass is defined
without import, which gives a clue that PythonWin is not running the
script independant of the interactive window.

Or try adding the following to the end of your chap2.py:
print "Hello World"
somevar = 12345
And run in PythonWin and see what happens to your interactive window
and if somevar is defined.

If you close and open PythonWin and use the interactive window without
having first run chap2.py, you will find it behaves the same as the
mac.

Ivan
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1911
by: engsolnom | last post by:
Knowing absolutely zero about benchmarks, I was interested in the posting referencing the paper by Dr. Cowell-Shah. I was curious how long Python takes just to read and display time.clock(), soI made the simple test below. I was amazed at the difference in speed depending on when/how the clock times were converted to milliseconds....almost 4...
1
2527
by: barnesc | last post by:
Hi! Here's a simple hashcash implementation in Python. 28 lines of actual code. Can be reduced to 17 lines for instructional purposes, if you don't want clustering, and use xrange() instead of irange().
38
3479
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser = serial.Serial()
5
2986
by: max(01)* | last post by:
hello. i wrote a very simple tkinter demo program that uses menus, buttons, labels, entries, frames and secondary toplevels. it is a python version of a java program made by a colleague. the user can create ("Scrivi") a record with his second name, first name and date of birth, save ("Salva") the record to a file or read in ("Leggi") a...
7
1659
by: Dustin MacDonald | last post by:
Hi everyone. This is my first time posting to this newsgroup, and although I maintain my netiquette I might've missed something specific to the newsgroup, so hopefully you can avoid flaming me if I have :) I apologize for the length of this post but I figure the more information the better. My problem is that I'm getting a syntax error...
1
2208
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner: http://groups.google.com/group/comp.lang.python/browse_thread/thread/58a01273441d445f/ also pointed to a simple eval function by Fredrik Lundh:...
5
1409
by: sstidham | last post by:
I cannot understand for the life of me why this isn't working, but maybe someone can help me out. It should be a simple Sum. I have a table that is imported data from Excel. I need to total up different, for lack of a better word, Columns to get a number of HSD sales for each Agent. Here is how I currently have the query: SELECT .,...
7
1681
by: bvdp | last post by:
Okay guys. I have the _ast based safe eval installed and working in my program. It appears to be working just fine. Thanks for the help. Now, a few more questions: 1. I see that _ast is a 2.5 module?? So, for folks using my code with <2.5 I could do something like this: # I've got some imports here to look after the error() and...
13
2069
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to teach myself this aspect of Python by working up a trial project, part of which calls for pulling in data from a serial data connection at regular...
0
7459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5965
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5322
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.