Very simple code works on PC but not Macbook

Discussion in 'Mac Programming' started by Windy Miller, Mar 18, 2010.

  1. Windy Miller macrumors newbie

    Joined:
    Mar 18, 2010
    Location:
    Newbury, UK
    #1
    My 13 year old is the proud owner of a MacBook Pro, and I want him to use it for more than just Facebook, so we've got a copy of "Python Programming for the Absolute Beginner" (I liked the idea of learning through coding games) and we're working through it together, me on my PC, him on his MacBook. We hit a problem in Chapter 1.

    The code couldn't be more basic:

    print ("game over")
    input ("\n\nPress the enter key to exit.")

    When I find the file in Windows and double click it opens a console window, displays the two lines of text, then closes the window when you press the enter key, exactly as it should. The problem (and I realise this I'm probably showing my ignorance of things apple here) is that I don't know what the equivalent operation would be on the MacBook.

    When my son locates the file in finder and clicks on "open using python launcher" he gets the following:

    Last login: Thu Mar 18 18:15:11 on ttys000
    Gareth-Carlesss-MacBook-Pro:~ garethcarless$ cd '/Users/garethcarless/Documents/Python Code/' && '/usr/bin/pythonw' '/Users/garethcarless/Documents/Python Code/game_over.py' && echo Exit status: $? && exit 1

    Game Over


    Press the enter key to exit.

    Traceback (most recent call last):
    File "/Users/garethcarless/Documents/Python Code/game_over.py", line 2, in <module>
    input("\n\nPress the enter key to exit.")
    File "<string>", line 0

    ^
    SyntaxError: unexpected EOF while parsing
    Gareth-Carlesss-MacBook-Pro:python Code garethcarless$ ​

    So clearly something's not happening as it should. Maybe what he's doing isn't the right way to run an executable file on a Mac. What's frustrating is that I can help him with the programming but I have just about zero experience with Macs.

    Needless to say any advice would be (very) gratefully received!
     
  2. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #2
    On his MBP, open /Applications/Terminal.app and type

    Code:
    python
    What shows up?
     
  3. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    The correct path is actually ~/Applications/Utilities/Terminal.app

    Also, I can assure the OP that Python will run fine on a Mac, so there's no need to worry (it just may take a sec to get it setup properly--although it should be ready-to-roll from the start).
     
  4. Windy Miller thread starter macrumors newbie

    Joined:
    Mar 18, 2010
    Location:
    Newbury, UK
    #4
    When you do what you described, opening the terminal box ("Terminal - bash - 80x24" at the top) and entering "python", you get several lines of text starting with "Last login" and ending with the >>> prompt.

    I'm not sure that I'm making a good job of explaining the problem here. He can use IDLE and operate in script and interactive mode, but I'm not sure how he should run programs from outside python. Running the simple program above in script mode ("Run" & "Run module") opens a new window ("Python Shell" at the top) and prints "Game Over" and "Press the enter key to exit" but then when you press the enter key it doesn't actually exit, it just gives the >>> prompt on the next line. This is different to what happens when I run it using windows when a new window appears then disappears when you press exit.

    Thanks for the help and also any future help. It's way after midnight here in the UK so I'll be reading any replies in the morning!
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    I know nothing about python, but apparently the problem is that input isn't what you think it is.

    it tries to evaluate the input as a python script and thus fails if what you enter isn't.

    try raw_input instead.

    http://en.wikibooks.org/wiki/Python_Programming/Input_and_output

    Code:
    print ("game over")
    x=raw_input ("\n\nPress the enter key to exit.")
    EDIT: I just tried that as a text file with vi and run with the Python launcher from the GUI and all the stuff after Traceback in your original output is gone, replace with "Exit status: 0"

    B
     
  6. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #6
    type python in the terminal, then type a space, then drag and drop the file onto the terminal (or type out the path, whatever is easiest).

    Then hit return.

    The output from your program will be displayed in the console.

    Which text editor/IDE is your Son using right now?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Copy and paste the output into your reply here. He's trying to find out what version of python is being run. The output from the pyton command includes the version number. If you don't post it, we can't tell what version you have.

    Also please post your Mac OS version number. This can be seen by choosing "About This Mac" from the Apple-shaped menu located at top-left of screen.


    Running your example program, I see the same failing output as you posted.

    After googling and reading the Python functions reference, it warns that the input function expects a valid Python expression. Apparently, an empty line is not a valid expression.

    http://docs.python.org/library/functions.html

    Search for the word input on that page. Note its suggestion to use raw_input instead. If I change your code to be as follows, it works for me on my Mac:
    Code:
    print ("game over")
    raw_input ("\n\nPress the enter key to exit.")
    
    I also got your original to work by enter 42 then pressing Enter (because 42 is a valid Python expression).
     
  8. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    I wonder if this is also true in the OP's Windows environment as well, and was just not noticed due to the default behavior of the console box in windows to disappear unceremoniously when execution is done.

    B
     
  9. Windy Miller thread starter macrumors newbie

    Joined:
    Mar 18, 2010
    Location:
    Newbury, UK
    #9
    Apologies for not providing the info required, more haste less speed.

    Python 3.1.1 (r311:74543, Aug 24 2009, 18:44:04)
    [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    Type "copyright", "credits" or "license()" for more information.
    >>> ​

    Mac OSX 10.6.2

    Editing text in IDLE.

    The problem with the "raw_inputs" suggestions is that this function doesn't exist in python 3.1. From the wikibooks reference above:

    Note: in 3.x "...raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input())."​

    which obviously none of you knew was relevant because I hadn't told you what version of python I was using.

    Balamw's last comment seems to make sense. The pc and the mac behave in the same way when I run the script from within the python shell, ie: the two lines of text are printed then pressing enter gives me the >>> prompt. It's only when I run from "outside" python that I get a problem. I originally thought that this was because I didn't know how to run executable files properly on the Mac, but maybe it's actually just a syntax problem which the pc doesn't display but the Mac does. We need to press on and see if something similar happens with other scripts as we move through the book.

    I learned to program at school in the early 1980s using BASIC on a Commodore PET and I still think that was a sound approach. I don't know what it's like in the USA but here in the UK programming seems to have more or less dropped off the curriculum. What schools call IT studies really just means learning how to use MS office.
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16)

    Are you sure the loader is using the 3.x you've got on the Mac? And not an earlier version?

    We seem to be of similar vintage I learned BASIC and Logo on Apple ][s right around '80.



    B
     
  11. ViViDboarder macrumors 68040

    ViViDboarder

    Joined:
    Jun 25, 2008
    Location:
    USA
    #11
    I'm not sure why this is getting so confusing. I'm pretty sure that your code is working.

    What is happening is that Windows is launching a CMD prompt in a window that will only be open for the life of the process. When you run in the Python Shell, the shell is opening, running your code and then the window stays open. If you are getting the >>> line, that just means it's ready for the next command.

    You also have the option to run python from your terminal. Meaning you can open Terminal.app and navigate to the folder with the python source and type
    Code:
    python ./myFile.py
    and it should run as well. In this case, as with the first, you will see the script run and then when you hit enter you will be back at the command line.

    If you want a window to open and then close when complete you can try to write a .sh script that will execute python for you.

    To do this open a text editor and edit this text to have your file paths and save it as RunMyFile.sh. Then you can execute this file and it will run the python script and then be done.

    Code:
    #! /bin/bash
    
    python ~/Documents/MyPythonFile.py
    
    The ~ is the current user's home directory.
     
  12. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #12
    Code:
    Traceback (most recent call last):
    File "/Users/garethcarless/Documents/Python Code/game_over.py", line 2, in <module>
    input("\n\nPress the enter key to exit.")
    File "<string>", line 0
    
    ^
    SyntaxError: unexpected EOF while parsing
    This part is behaving exactly like 2.6.1 on my system, the syntax error being generated by the use of input instead of raw_input.

    So now the question seems to be "python" seems to pull up 3.1.1, but is that the same version that the GUI launcher is using at /usr/bin/pythonw (as per the output).

    can you add
    Code:
    import sys
    sys.version
    to the top pf the script and try running it again the way you got the Traceback/Syntax Error ?

    B
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    I have 10.6.2, but the python version is 2.6.1.
    Code:
    Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    
    Do you have anything non-standard installed on your computer, like fink, MacPorts, or something else that might have altered /usr/bin/python?

    Oh, and here's another command to copy and paste into Terminal:
    Code:
    which python
    
    Paste the output into a post.
     
  14. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #14
    Same thing happens in Linux.

    It may be that EOL is two characters in Windows ("\r\n") whereas it's a single character "\n" in *nix systems.

    Edit: from python docs - http://docs.python.org/library/functions.html
     
  15. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #15
  16. splitpea macrumors 6502a

    Joined:
    Oct 21, 2009
    Location:
    Among the starlings
    #16
    So do most interpreted languages (see also: Javascript, PHP, Perl, Ruby). It's always the developer's responsibility to be security-aware and not to execute untrusted input.
     
  17. Windy Miller thread starter macrumors newbie

    Joined:
    Mar 18, 2010
    Location:
    Newbury, UK
    #17
    OK. I added the two lines suggested by balamw, located the file using finder and clicked "open with python launcher (3.1.1)". The output was:

    chown22 - nothing non-standard at all. Inserting

    Code:
    which python
    
    gave me an "invalid syntax" message with "python" highlighted.

    I do realise that in the scheme of things this is trivial, but when I'm learning how to do something new I like to understand what's going on. Your comments are really appreciated but I would entirely understand if you all have better things to do!

    (I'm going to be offline Saturday / Sunday - not just ignoring any posts.)
     
  18. davbran macrumors newbie

    Joined:
    Feb 5, 2012
    #18
    To the OP,

    While you might have sussed this out by now, I thought I would put in the answer I have found in my environment, linux. The code in the book you're using is based on Python3. If Python3 isn't installed on your son's Mac and or used to execute the application, then you will receive the error you have received.

    I fixed this by running entering the following at the command line.

    Code:
    python3 game_over.py
    This resolved the issue for me.

    I hope this helps any other beginners :)
     
  19. jared_kipe, Feb 6, 2012
    Last edited: Feb 6, 2012

    jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #19
    The correct path is actually:
    /Applications/Utilities/Terminal.app

    ;)
     
  20. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #20
    That brainfart from 2 years ago still stinks lol.

    *hangs head in shame*
     
  21. amorya macrumors 6502

    Joined:
    Jun 17, 2007
    #21
    Gah, didn't notice thread necromancy. Message redacted.
     

Share This Page