OK, I am very new in programming, and I installed python2.5.1 and pygame, and I wrote the hello world programme etc. and I ran it in the python shell. But the problem is, I can't run any python document outside the shell. I tried an example in pygame, and the examples in python, but the same happens. A brief shell opens for a few milliseconds, the closes. Why does this happen and can I correct it?

I figured it out!

For others who might run into this problem, a simple solution would be to append the line

raw_input("Press ENTER to exit")

at the end of your code.

Agreed, because then if an exception occurs you can view it in the IDE, as opposed to my method where the window just closes...

To correct this problem you can add this to the end of your program:

_pause = raw_input("Press <enter> to continue...")

If you're like me and don't want to add this to all your programs, I have created a small python program that will automate the process and then ask you if you want to remove the code.

To use it put it in the same directory as your program and run it. Type in the filename of your program (exclude the .py extension, this will be automatically added) and the program will add the code, launch the program and then ask you if you want to remove the code.

This probably won't work with all programs and I'm not to sure if it'll run on Linux or Mac, though I have tried my best to make it as portable as possible.

PythonPauser.py

from os import system
from os import remove
from shutil import copyfile

program = raw_input("Which program do you want to open? ") + ".py"
progtmp = program + ".tmp"

copyfile(program, progtmp) ##Backup original file

pause = open(program, "a")
pause.write("_pause = raw_input('Press <enter> to continue...')") 
pause.close()

system(program) ##Launch the program

restore = raw_input("Do you want to remove the pausing code from the program?(y\n) ")
if restore == "y":
    copyfile(progtmp, program)
    remove(progtmp)
else:
    pass
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.