Skip to content Skip to sidebar Skip to footer

Stop Windows From Closing Python

This question may have been asked a couple of times but I cannot seem to find it. Basically I am just learning Python and I am on Windows, this means I double click the .py file an

Solution 1:

In order to keep your program intact, e.g. to not introduce unwanted catch-em-all exception handling (aka pokemon handling) there are at least three options:

  1. Use any console terminal, e.g. built-in cmd or powershell or any third-party console apps out there.
  2. Use any IDE: pycharm, IDLE (python windows installer by default sets it up) or whatever you have capable of running python code.
  3. Use text editor plugins for running python code. At least notepad++ and sublime text are capable of doing so.

I would recommend starting with option 1 for starters, then slowly move to option 3 for small scripts and projects, and option two for larger ones.

Solution 2:

I you put an input function at the bottom of your script then it will hang there until you hit enter or close the command prompt. If you call an exit function put it immediately before the exit function is called. Otherwise place it at the bottom of the script.

Also I assume you have defined is_int already in your script?

Solution 3:

What would you think it should do?

Python is drawing the window you see, if python crashes, the windows is going away.

You can run it trough cmd, or within an IDE. (like IDLE, that has some problem though when it comes to GUI)

Otherwise, add something like this at the end of the file

try:
     run() 
except Exception as inst:
     printtype(inst), inst.args
     #it prints the exceptionprint sys.exc_traceback.tb_lineno 
     #if you want the line number where the error occurred in the source code
     raw_input()

inst is the exception instance, you can see the type and the list of arguments. Then with the sys module you can also see the line where the error occurred in the code.

This way every error will be handled and displayed before closing

Is this the right way?

No. You should really be using ad IDE (like Eclipse with PyDev or PyCharm).

Solution 4:

After @SeçkinSavaşçı's extremely useful comment at the start:

The most common way is to let it wait for an input, then ignore the input and terminate the script.

Which took me a second to understand I went in search of how to do this, so first I saw to stop the script and used:

while(True):
    try:
        # Application code hereexcept:
        input('')

Which worked really well to catch all errors, which unlike in PHP (which I have become comfortable with unfortunately) are all exceptions.

So the next part was to to tell me what error had occured and how to fix it, I needed a backtrace. It just so happens that the Python docs gave me the answer right here: http://docs.python.org/2/library/traceback.html#traceback-examples in an easy to see example:

exc_type, exc_value, exc_traceback = sys.exc_info()
print(traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout));

Add that above the input('') and I had my perfect error handling showing me everything I needed.

Thanks all,

Solution 5:

Try import time and time.sleep(). Also, I recommend you to use IDLE or Geany. I've been using them and they work out well.

Post a Comment for "Stop Windows From Closing Python"