How to exit a script in Spyder?

One can

  • exit the script by raising a custom exception like

    raise Exception('exit')
    

    or

  • encapsulate the code into a function (e.g. main) and use return inside.

If one doesn't want to change the script, one can

  • Switch to "Execute in a new dedicated Python interpreter" or

  • register an exit handler at the IPython console:

    def exit_handler(): raise Exception("exit()"), get_ipython().ask_exit = exit_handler
    

As Robert Pollak suggest, the comment in Spyderlib Issue 1974 #4 is a better solution. Just define a function which cause an exception, and call this function if you want to stop the execute of script inside spyder.

def f(): raise Exception("Found exit()")

Tags:

Python

Spyder