Issue
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?
Solution
Update: In at least Spyder 5.0.3, the call to sys.exit()
does not kill the kernel any more! (Thanks to @bhushan for the info!)
For earlier versions, the following still holds:
To exit the script, one can
raise a silent exception with
raise SystemExit(0)
(without traceback)or a custom exception like
raise Exception('my personal exit message')
or encapsulate the code into a function (e.g.
main
) and usereturn
inside.
If one wants to keep the call to exit()
in 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
Answered By - Robert Pollak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.