25

In Jupyter notebook, if I have

print("hello") quit() 

in the first cell and

print("Good bye") 

in the second, when I do "Run all" it prints hello and also Good bye.

In other words, quit() seems to only stop the execution of the cell itself, not the whole script.

Is there some way to add a breakpoint to your code so that it stops executing when it gets to it?

1

6 Answers 6

41

You can use IPython.core.debugger.Pdb to invoke pdb. Insert the following line to set a breakpoint.

from IPython.core.debugger import Pdb; Pdb().set_trace() 

See this page for debugger commands.

Also, you can use %debug magic to invoke pdb after an error. Just type %debug to the next cell if an exception occurs. Then pdb runs from its stack frames.

Sign up to request clarification or add additional context in comments.

Comments

19

From the JupyterLab docs:

JupyterLab 3.0 now ships with a Debugger front-end by default.

For the debugger to be enabled and visible, a kernel with support for debugging is required.

If you are using Pip, install the Xeus-Python kernel with pip install xeus-python.

Then open a jupyterlab notebook, and choose the new kernel from the toolbar:

Selecting Kernel

You can then debug your code as follows:

Debugging in JupyterLab

Source: https://jupyterlab.readthedocs.io/en/stable/user/debugger.html

4 Comments

Note for self, instead of running jupyter notebook, run jupyter-lab. this is a different interface and allows debugging. The old one does not.
Great news. Any recommended kernal for Conda?
I think xeus-python is also available in Conda. Try: conda install -c conda-forge xeus-python
Now you do not need any special kernel but you have to enable the debugger using the bug icon (on the right side of the notebook's top bar). I am using the debugger in JupyterLab 3.4, Python 3.10.
14

As far as I know, jupyter notebook doesn't have a breakpoint function however you can add something like:

assert False, "breakpoint" 

where you want to stop although it isn't very elegant.

3 Comments

I simply use 0/0 which throws a ZeroDivisionError and stops the execution
@Akronix that would be simple to use, but confusing for anyone who may look into your code, his answer is descriptive
Or one can similarly do: raise ValueError('breakpoint')
6
  • Ensure that you have selected the kernel (e.g., Python 3) on the upper right corner of the coding window
  • Select the debugging icon on its left (as you select it, each line will be numbered)
  • Now you can select breakpoints, by clicking on the line number.

1 Comment

Any recommended kernal for Conda?
5

Jupyter Notebook has visual debugger just like Jupyter Lab since version 7.0.0, released in July 2023. You can use it exactly like abrac showed in his answer. Just make sure that you enabled the debugger by clicking on the small bug icon on top right corner and optionally display the debugger panel (with Ctrl + Shift + E shortcut or using top bar: View > Debugger Panel).

Comments

-7

You can probably try exit() but I am not sure if that's exactly what you need.

1 Comment

That does exactly the same as using quit() as far as I can tell. In other words it doesn't stop the rest of the script from running.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.