132

As I know, %debug magic can do debug within one cell.

However, I have function calls across multiple cells.

For example,

In[1]: def fun1(a)
           def fun2(b)
               # I want to set a breakpoint for the following line #
               return do_some_thing_about(b)

       return fun2(a)

In[2]: import multiprocessing as mp
       pool=mp.Pool(processes=2)
       results=pool.map(fun1, 1.0)
       pool.close()
       pool.join

What I tried:

  1. I tried to set %debug in the first line of cell-1. But it enter into debug mode immediately, even before executing cell-2.

  2. I tried to add %debug in the line right before the code return do_some_thing_about(b). But then the code runs forever, never stops.

What is the right way to set a break point within the ipython notebook?

Trooper Z
  • 1,132
  • 11
  • 26
Rex
  • 1,907
  • 5
  • 12
  • 18

10 Answers10

96

You can use ipdb inside jupyter with:

from IPython.core.debugger import Tracer; Tracer()()

Edit: the functions above are deprecated since IPython 5.1. This is the new approach:

from IPython.core.debugger import set_trace

Add set_trace() where you need a breakpoint. Type help for ipdb commands when the input field appears.

rayryeng
  • 96,704
  • 21
  • 166
  • 177
Leopd
  • 37,882
  • 29
  • 117
  • 156
  • 21
    `Tracer` was depreciated. Now it works this way: `from IPython.core.debugger import set_trace` and `set_trace()` evokes a breakpoint. Source: https://davidhamann.de/2017/04/22/debugging-jupyter-notebooks/ – Anton Tarasenko May 05 '17 at 14:53
  • 2
    Handy to use as one line, wherever needed: `from IPython.core.debugger import set_trace;set_trace()` – Nir Feb 26 '20 at 11:17
76

Use ipdb

Install it via

pip install ipdb

Usage:

In[1]: def fun1(a):
   def fun2(a):
       import ipdb; ipdb.set_trace() # debugging starts here
       return do_some_thing_about(b)
   return fun2(a)
In[2]: fun1(1)

For executing line by line use n and for step into a function use s and to exit from debugging prompt use c.

For complete list of available commands: https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf

Willian Fuks
  • 9,489
  • 9
  • 35
  • 65
Tevin Joseph K O
  • 2,116
  • 17
  • 23
  • ipdb does't give me the debug prompt in the other cell with the sample code mentioned in the question. Now the code keeps running forever. – Rex Sep 08 '15 at 19:41
  • @Rex For me it works. Where did you put ipdb? Put import ipdb; ipdb.set_trace(); before the line return do_some_thing_about(b). Then, call fun1(a) from second cell, it should work. For exiting the debugging promt use 'c' – Tevin Joseph K O Sep 09 '15 at 15:00
  • @Rex Updated the answer for more clarity. – Tevin Joseph K O Sep 09 '15 at 15:07
  • 31
    This works in iPython console but not in iPython notebook (which is what the question asked). When I type `import ipdb; ipdb.set_trace()` within a notebook cell it says `MultipleInstanceError: Multiple incompatible subclass instances of TerminalIPythonApp are being created.` – Bill Sep 15 '16 at 01:42
  • 2
    new to jupyter...it seems that it is more suitable for presentation and sharing code snippets...can we have functionality that IDEs (like eclipse, rstudio) provide like actual debugging, step through, breakpoints, adding watch to the expression and variables etc. At first, it seems that it is not the case, just want to be sure..... – Mahesha999 Mar 15 '17 at 07:30
  • Very weird for me. Seems to work sometimes but not everytime. And if you forget to quit properly the debugger, you can stay blocked in it and be forced to restart the kernel. Definitely not an ideal solution... – Jean Paul Oct 17 '18 at 14:07
18

In Python 3.7 you can use breakpoint() function. Just enter

breakpoint()

wherever you would like runtime to stop and from there you can use the same pdb commands (r, c, n, ...) or evaluate your variables.

Vlad Bezden
  • 59,971
  • 18
  • 206
  • 157
  • 3
    To add to this, when you call breakpoint(), you can type 'help' in the text box that appears to get a list of commands. – gbeaven Nov 29 '18 at 17:49
  • There seems to be a persistent issue of hang-up after using breakpoint() or pdb: https://github.com/ipython/ipython/issues/10516 – Dave Liu Nov 07 '19 at 19:01
16

Your return function is in line of def function(main function), you must give one tab to it. And Use

%%debug 

instead of

%debug 

to debug the whole cell not only line. Hope, maybe this will help you.

12

You can always add this in any cell:

import pdb; pdb.set_trace()

and the debugger will stop on that line. For example:

In[1]: def fun1(a):
           def fun2(a):
               import pdb; pdb.set_trace() # debugging starts here
           return fun2(a)

In[2]: fun1(1)
krock
  • 26,870
  • 12
  • 71
  • 83
  • 1
    @Rex Not necessarily. [`ipdb`](https://pypi.python.org/pypi/ipdb) is a refactor of the Python Debugger that is more closely integrated with IPython. [`pdb`](https://docs.python.org/2/library/pdb.html) is built in. – Two-Bit Alchemist Sep 09 '15 at 15:25
  • `ipdb` can also be used outside the ipython shell and comes with some conveniences like tab completion. – Jan Dec 14 '16 at 15:01
9

After you get an error, in the next cell just run %debug and that's it.

Escachator
  • 1,283
  • 1
  • 10
  • 26
8

Just type import pdb in jupyter notebook, and then use this cheatsheet to debug. It's very convenient.

c --> continue, s --> step, b 12 --> set break point at line 12 and so on.

Some useful links: Python Official Document on pdb, Python pdb debugger examples for better understanding how to use the debugger commands.

Some useful screenshots: enter image description hereenter image description here

flowera
  • 689
  • 8
  • 11
  • Good answer. However, I think (I am new to Jupyter notebook) one should add %debug to set a breakpoint. – Ad Infinitum Oct 26 '17 at 12:31
  • What I do is copy and paste `pdb.set_trace()` to where I want to set a breakpoint, since the `b line_no` is not working in Jupyter Notebook and works well on python IDLE. – flowera Oct 26 '17 at 20:04
  • Thanks! I also uploaded some screenshots that I tested today. If I could switch over to Python IDLE I would love to do so.. – flowera Oct 26 '17 at 21:09
  • Also, I find a way to show line number on jupyter notebook, click new line, and then press l.Of course you can also program your own shortcut, links are available on the jupyter notebook website. – flowera Nov 08 '17 at 19:53
  • 1
    I use always "p" to show the list of commands in the Jupyter notebook. When I write "show line numbers", the command appears there witt the shortcut next to it. You can also learn the shortcut of the command, which you want to use. – Ad Infinitum Nov 10 '17 at 10:38
7

The %pdb magic command is good to use as well. Just say %pdb on and subsequently the pdb debugger will run on all exceptions, no matter how deep in the call stack. Very handy.

If you have a particular line that you want to debug, just raise an exception there (often you already are!) or use the %debug magic command that other folks have been suggesting.

waterproof
  • 3,916
  • 4
  • 25
  • 27
6

I just discovered PixieDebugger. Even thought I have not yet had the time to test it, it really seems the most similar way to debug the way we're used in ipython with ipdb

enter image description here

It also has an "evaluate" tab

Community
  • 1
  • 1
Alessandro Dentella
  • 800
  • 1
  • 9
  • 23
1

A native debugger is being made available as an extension to JupyterLab. Released a few weeks ago, this can be installed by getting the relevant extension, as well as xeus-python kernel (which notably comes without the magics well-known to ipykernel users):

jupyter labextension install @jupyterlab/debugger
conda install xeus-python -c conda-forge

This enables a visual debugging experience well-known from other IDEs.

enter image description here

Source: A visual debugger for Jupyter

fuglede
  • 14,583
  • 2
  • 38
  • 72
  • 1
    xeus-python doesn't run on my Windows 10. Check my issue [xeus-python issue on github](https://github.com/jupyter-xeus/xeus-python/issues/239#issuecomment-606737348) – sergzemsk Apr 10 '20 at 17:40