How to exit a script in Spyder?

霸气de小男生 提交于 2020-01-03 12:02:25

问题


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?


回答1:


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
    



回答2:


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()")


来源:https://stackoverflow.com/questions/25928377/how-to-exit-a-script-in-spyder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!