Question:
Python Programs Help!?
2012-09-26 08:33:40 UTC
I've made a Python program ( I'm on Ubuntu, but also have WIn7 ) and when i run it, it opens Terminal and it closes instantly, do I need to do something called "compiling" and how do I do that? ***I'm new to programming*** :)
Three answers:
husoski
2012-09-26 10:12:16 UTC
You should open up a terminal window and run the program from the bash command line. If you try to run from the File Browser window, Ubuntu will open a terminal window just long enough to run the program, and then close the window. As you saw.



Navigate to the directory where you saved the file (your home folder, or perhaps a separate folder like pythonwork\ if you want to make one. Then run using the python command:



user@machine:~$ python myprogram.py



You can put in a prompt at the end, using raw_input for Python 2, or just plain input for Python 3, but that won't execute if your program dies from an uncaught exception. You really do want to see those error messages--especially when you are learning. Even so, you'll probably have to do two things to make the python source directly runnable.



1. Make the file executable. Either right-click on the icon in the File Browser and click Properties..., or run the bash command: chmod +x myprogram.py



2. Add a "shebang" line. Add the following as the very first line of your source:



#!/usr/bin/python



The "shebang" only works on Unix/Linux shells (like bash in Ubuntu.



If you have a GUI-based editor that you like, that's no problem. Just remember to save the changes to the file before you switch windows to run and test those changes.



If you like working in a windowed environment then, after you are comfortable with Python basics, take a look at tkinter, or one of the other GUI frameworks. There's a long list at:

http://wiki.python.org/moin/GuiProgramming
2016-12-18 15:40:57 UTC
in case you have examine something approximately sound in "Console-Mode" Python, that's because of the fact there is not any sound function with Console Python. you ought to examine wxPython or PyGame or possibly even Tkinter (i think of helps sound advantageous factors)
satish
2012-09-26 08:38:51 UTC
in windows 7 the program is executing and then exiting.



you can either:

run the program from command line

python



or add something like this to end of program

raw_input("Press any key to exit")


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...