I'd like to programmatically exit a cell early in IPython Notebook. exit(0)
, however, kills the kernel.
Whats the proper way to do this? I'd prefer not to split the cell or manually halt execution.
Just import 'exit' from the code beneath into your jupyter notebook (IPython notebook) and calling 'exit()' should work. It will exit and letting you know that...
Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.
IPython recognizes objects of this type, so when a line containing just exit or quit is executed, IPython actually exits. A Jupyter notebook's IPython kernel has exit and quit set to instances of the very closely related IPython.
Shift + Enter run the current cell, select below. Ctrl + Enter run selected cells. Alt + Enter run the current cell, insert below. Ctrl + S save and checkpoint.
Slightly more "proper" options:
This will get you out of all but the worst try/except blocks.
raise KeyboardInterrupt
A little cleaner version of yours:
assert(False)
or simply:
raise
if you want to save a couple keystrokes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With