Simple import question about mac osx

s0suk3 at gmail.com s0suk3 at gmail.com
Tue Apr 29 13:54:18 EDT 2008


On Apr 29, 12:46 pm, jmDesktop <needin4mat... at gmail.com> wrote:
> On Apr 29, 1:16 pm, jmDesktop <needin4mat... at gmail.com> wrote:
>
>
>
> > 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().



More information about the Python-list mailing list