How to exit a script in Spyder?

2019-06-23 00:39发布

I am using WinPython's Spyder. When I am creating a new script with the command exit(), and run it there, this command kills the kernel:

It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console.

What's the right way to stop a script in this runtime environment?

标签: python spyder
2条回答
相关推荐>>
2楼-- · 2019-06-23 01:20

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()")
查看更多
一夜七次
3楼-- · 2019-06-23 01:22

One can

  • exit the script by raising a custom exception like

    raise Exception('exit')
    

    or

  • encapsulate the code into a main function 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
    
查看更多
登录 后发表回答