Ответ 1
Я переписываю свой ответ из здесь, потому что решение должно также применяться к вашему вопросу. Это будет...
- не убивать ядро при выходе
- не отображается полная трассировка (нет трассировки для использования в оболочке IPython)
- не заставлять вас вводить код с помощью try/excepts
- работает с IPython или без него, без изменений в коде
Просто импортируйте 'exit' из кода ниже вашего ноутбука jupyter (ноутбук IPython) и вызывается 'exit()'. Он выйдет и даст вам знать, что...
An exception has occurred, use %tb to see the full traceback.
IpyExit
"""
# ipython_exit.py
Allows exit() to work if script is invoked with IPython without
raising NameError Exception. Keeps kernel alive.
Use: import variable 'exit' in target script with
'from ipython_exit import exit'
"""
import sys
from io import StringIO
from IPython import get_ipython
class IpyExit(SystemExit):
"""Exit Exception for IPython.
Exception temporarily redirects stderr to buffer.
"""
def __init__(self):
# print("exiting") # optionally print some message to stdout, too
# ... or do other stuff before exit
sys.stderr = StringIO()
def __del__(self):
sys.stderr.close()
sys.stderr = sys.__stderr__ # restore from backup
def ipy_exit():
raise IpyExit
if get_ipython(): # ...run with IPython
exit = ipy_exit # rebind to custom exit
else:
exit = exit # just make exit importable