Simple import question about mac osx

Discussion in 'Python' started by jmDesktop, Apr 29, 2008.

  1. jmDesktop

    jmDesktop Guest

    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.
     
    jmDesktop, Apr 29, 2008
    #1
    1. Advertising

  2. jmDesktop

    jmDesktop Guest

    On Apr 29, 1:16 pm, jmDesktop <> 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.
     
    jmDesktop, Apr 29, 2008
    #2
    1. Advertising

  3. jmDesktop

    Guest

    On Apr 29, 12:46 pm, jmDesktop <> wrote:
    > On Apr 29, 1:16 pm, jmDesktop <> 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().
     
    , Apr 29, 2008
    #3
  4. jmDesktop

    jmDesktop Guest

    On Apr 29, 1:54 pm, wrote:
    > On Apr 29, 12:46 pm, jmDesktop <> wrote:
    >
    >
    >
    >
    >
    > > On Apr 29, 1:16 pm, jmDesktop <> 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().- 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.
     
    jmDesktop, Apr 29, 2008
    #4
  5. jmDesktop

    Jerry Hill Guest

    On Tue, Apr 29, 2008 at 2:14 PM, jmDesktop <> wrote:
    > 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
     
    Jerry Hill, Apr 29, 2008
    #5
  6. jmDesktop

    jmDesktop Guest

    On Apr 29, 2:37 pm, "Jerry Hill" <> wrote:
    > On Tue, Apr 29, 2008 at 2:14 PM, jmDesktop <> wrote:
    > >  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.
     
    jmDesktop, Apr 29, 2008
    #6
  7. jmDesktop

    Guest

    On Apr 29, 2:17 pm, jmDesktop <> wrote:
    > On Apr 29, 2:37 pm, "Jerry Hill" <> wrote:
    >
    >
    >
    > > On Tue, Apr 29, 2008 at 2:14 PM, jmDesktop <> wrote:
    > > > 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.
     
    , Apr 29, 2008
    #7
  8. jmDesktop

    Jerry Hill Guest

    On Tue, Apr 29, 2008 at 3:17 PM, jmDesktop <> wrote:
    > 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>python -i chap2.py
    >>> foo1=FooClass()

    Created a class instance for John Doe
    >>>


    and doing this:
    C:\Python25>python
    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.FooClass()

    Created a class instance for John Doe
    >>>


    --
    Jerry
     
    Jerry Hill, Apr 29, 2008
    #8
  9. jmDesktop

    ivan Guest

    On Apr 29, 3:47 pm, "Jerry Hill" <> wrote:
    > 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>python -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
     
    ivan, Apr 29, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Tomson

    EDA apps on Mac OSX?

    Phil Tomson, Jul 9, 2004, in forum: VHDL
    Replies:
    0
    Views:
    1,114
    Phil Tomson
    Jul 9, 2004
  2. Wesley Groleau

    Re: Perl in Mac OSX

    Wesley Groleau, Jul 28, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,035
    Wesley Groleau
    Jul 28, 2003
  3. darrel

    Re: Mac OSX problem

    darrel, Sep 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    327
    Steven Cheng[MSFT]
    Sep 23, 2004
  4. Steven R.
    Replies:
    13
    Views:
    203
    Elliot Temple
    Jul 15, 2006
  5. Albert Schlef

    A quick Mac OSX question

    Albert Schlef, Jan 9, 2009, in forum: Ruby
    Replies:
    2
    Views:
    128
    Pascal J. Bourguignon
    Jan 9, 2009
Loading...

Share This Page