24

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

38

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.

17

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

5
  • 2
    This looks exciting!
    – Simd
    Commented Apr 26, 2021 at 10:40
  • 4
    Note for self, instead of running jupyter notebook, run jupyter-lab. this is a different interface and allows debugging. The old one does not. Commented Feb 21, 2022 at 16:08
  • 1
    Great news. Any recommended kernal for Conda? Commented Jun 1, 2022 at 9:39
  • I think xeus-python is also available in Conda. Try: conda install -c conda-forge xeus-python
    – abrac
    Commented Jun 7, 2022 at 8:33
  • 6
    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. Commented Jun 17, 2022 at 13:57
13

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
  • 3
    I simply use 0/0 which throws a ZeroDivisionError and stops the execution
    – Akronix
    Commented May 17, 2020 at 8:31
  • @Akronix that would be simple to use, but confusing for anyone who may look into your code, his answer is descriptive
    – 27px
    Commented Aug 5, 2022 at 9:12
  • Or one can similarly do: raise ValueError('breakpoint')
    – Pierz
    Commented May 20 at 13:21
4
  • 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
  • Any recommended kernal for Conda? Commented Jun 1, 2022 at 9:40
3

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).

-7

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

1
  • 1
    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.
    – Simd
    Commented Jun 27, 2018 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.