App Opens And Closes Suddenly

Discussion in 'Mac Programming' started by Macman1993, Aug 5, 2008.

  1. Macman1993 macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #1
    I JUST started learning python and I have a problem I am trying to run a very simple app made just to teach me the absolute basics. I managed to screw this up. When I double click my app it opens and closes. I wrote code into it that is supposed to stop this from happening. I really want to program but if I can't see my work then I kinda lose motivation please help. This is the code that makes up my app like I said very very basic
    Code:
    print "Game Over"
    
    raw_input("\n\nPress The Enter Key To Exit")
    
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I ran this from the python prompt (Open Terminal and type "python") and it worked fine. If you're wanting to be able to run this from the terminal by using the name of the file, you need to put:
    #! /usr/bin/python

    You might need to substitute the proper path to python. You will also need to make your script executable using chmod.

    What's probably easier at this point is to save your script then execute it with python from Terminal, something like:
    python test.py

    If you save your code as test.py. I tried it and your code seems to work fine.

    -Lee
     
  3. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #3
    Well the book I'm using "Python Programing for the absolute beginner" says it should open no problem just double clicking the app. I don't want to have to use terminal to do it. So why wont it just work like its supposed to?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    is .py associated with /usr/bin/python? If not, what is opening it?

    I don't mean to be a jerk, but just embrace the terminal now. If you plan to work on an OS, there's no way around it. The sooner you get your feet wet the better off you'll be.

    Ignoring the interactive python prompt seems to really be hamstringing yourself, too.

    -Lee
     
  5. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #5
    Well first thing first I have no idea what the /usr/bin/python thing is or what it does. I got this book this afternoon and know no programing. I didn't count on things not working. And the code is being opened as a app. I double click the code it gets made into an app and then the app doesn't work, it opens and closes before anything happens.
     
  6. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #6
    It doesn't work because you did a build applet (according to your other post...), but you are only using console output. If you built a GUI then build applet would work fine.

    Use the Terminal, or an editor that will run the script for you (for example TextMate)

    Or, if you insist, use something like iHook to double click/run the .py file (http://www.versiontracker.com/dyn/moreinfo/macosx/16668)
    (though you will have to add #!/usr/bin/python, and chmod +x your .py file in the terminal -- easier just to use the terminal)
     
  7. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #7
    O so nothing is messed up that text app just isn't advanced enough to actually launch? Alright that makes me happy because I would hate to have bought this book for nothing.
     
  8. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #8
    Or...Wing IDE 101 is an editor that will run python scripts inside of it for free.
    http://wingware.com/downloads

    (Never tried it though, and it looks like it uses X11 so it may not be so nice to use)
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    The vast majority of programming when you first start is Terminal based unfortunately. It takes quite a big leap in understanding to start writing GUIs, although Python is easier than some for that.
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    No matter what the book says, at this stage you are MUCH better off at the Terminal. You've stated that you've never done any of this before, so it's time to start getting used to programming tools. Spending time figuring out how to get python scripts to run in a usable way when you double-click them will not really be a transferable skill. Learning some things about the terminal will.

    Go to Applications->Utilities->Terminal, and drag it to your dock (you'll be needing it often). Click the dock icon, and you will be presented with a prompt. You will be in your home directory (/Users/yourusername). You can change directories to your Desktop or elsewhere using the command "cd Desktop". You need to change directories to where your code was saved. Once there, run:
    python myprogram.py

    Your code will run, and the text that is being printed will display on screen, and the program will wait for you to press enter before exiting.

    As you progress, being able to readily see compiler errors and warnings(not sure if there are warnings in python, but probably) at the terminal is critical. It may not be what you were expecting, it may not be exactly how the book says to do things, but versatility is one of the key traits it takes to be a programmer.

    Good luck. Don't get too frustrated about anything this early on. You might not progress as quickly as you'd hoped, things might not click right away, but just don't give up. Accept that the process is about learning, not necessarily what you're able to do right away. Obviously being able to see the output of your program is necessary to proceed, but beyond that it's going to take time to get things working. People here are willing to help out, even if some of us have never used python before.

    -Lee
     
  11. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #11
    Alright I did that but have 1 problem, when I type in the file location /users/Colin/Desktop/untitled.py terminal says "Permission Denied" Heres exactly what I did with terminal.

    Code:
    Last login: Wed Aug  6 11:18:31 on ttys000
    colin-murphys-macbook-pro:~ Colin$ cd desktop
    colin-murphys-macbook-pro:desktop Colin$ /Users/Colin/Desktop/untitled.py
    -bash: /Users/Colin/Desktop/untitled.py: Permission denied
    colin-murphys-macbook-pro:desktop Colin$ 
    
     
  12. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #12
    You need to type:

    python /path/to/file/somefile.py

    If that still does not work you need to cd to the correct directory and type:

    chmod +x somefile.py

    to make the file executable.
     
  13. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #13
    You do not need to make a file executable if you call the python program with your script as a parameter, however you do if you want to omit the python part.

    Also note that if you are in the same directory as your script, you can just do something like "python untitled.py"
     
  14. Nicolaius macrumors newbie

    Joined:
    Jan 4, 2009
    #14
    dude, the book you're using is for windows. That's why he can do it and you can't.
     

Share This Page