Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Macman1993

macrumors 6502
Original poster
Nov 23, 2007
337
16
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")
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Macman1993

macrumors 6502
Original poster
Nov 23, 2007
337
16
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Macman1993

macrumors 6502
Original poster
Nov 23, 2007
337
16
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.
 

sord

macrumors 6502
Jun 16, 2004
352
0
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)
 

Macman1993

macrumors 6502
Original poster
Nov 23, 2007
337
16
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.
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
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?

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Macman1993

macrumors 6502
Original poster
Nov 23, 2007
337
16
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$
 

sord

macrumors 6502
Jun 16, 2004
352
0
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.
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"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.