As I know,
%debug magic can do debug within one cell.
However, I have function calls across multiple cells.
In: 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: import multiprocessing as mp pool=mp.Pool(processes=2) results=pool.map(fun1, 1.0) pool.close() pool.join
What I tried:
I tried to set
%debugin the first line of cell-1. But it enter into debug mode immediately, even before executing cell-2.
I tried to add
%debugin 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?
Install it via
pip install ipdb
In: def fun1(a): def fun2(a): import ipdb; ipdb.set_trace() # debugging starts here return do_some_thing_about(b) return fun2(a) In: 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
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
set_trace() where you need a breakpoint. Type
ipdb commands when the input field appears.
Your return function is in line of def function(main function), you must give one tab to it.
to debug the whole cell not only line. Hope, maybe this will help you.
In Python 3.7 you can use breakpoint() function. Just enter
wherever you would like runtime to stop and from there you can use the same pdb commands (r, c, n, …) or evaluate your variables.
You can always add this in any cell:
import pdb; pdb.set_trace()
and the debugger will stop on that line. For example:
In: def fun1(a): def fun2(a): import pdb; pdb.set_trace() # debugging starts here return fun2(a) In: fun1(1)
After you get an error, in the next cell just run
%debug and that’s it.
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.
%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.