What is the correct way to debug in an iPython laptop?

As I know,% debug magic can do debugging in a single 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:

  • I tried setting% debug on the first line of cell-1. But it immediately goes into debug mode, even before executing cell-2.

  • I tried adding% debug to the line just before the code "return do_some_thing_about (b)". But then the code runs forever, never stops.

What is the correct way to set a breakpoint in an ipython laptop?

+46
python ipython ipython-notebook pdb
Sep 05 '15 at 4:53 on
source share
5 answers

Use ipdb

Install it through

 pip install ipdb 

Using:

 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) 

Use n to execute line by line and use s to switch to a function and use c to exit the debugging prompt.

Full list of available commands: http://frid.imtqy.com/blog/2014/06/05/python-ipdb-cheatsheet/

+25
05 Sep '15 at 6:13
source share

You can use ipdb inside jupyter with:

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




Edit : The above functions are deprecated with IPython 5.1. This is a 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 box appears.

+45
Nov 28 '16 at 22:46
source share

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

 %%debug 

instead

 %debug 

to debug an entire cell, not just a row. Hope this helps you maybe.

+8
Sep 05 '15 at 5:09
source share

You can always add this to any cell:

 import pdb; pdb.set_trace() 

and the debugger stops at this 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) 
+7
Sep 05 '15 at 5:30
source share

Just type import pdb into jupyter notepad and then use cheatsheet for debugging. It is very convenient.

c → continue, s → step, b 12 → set breakpoint on line 12, etc.

Some useful links: Python pdb white paper , python pdb debugger examples for a better understanding of how to use debugger commands .

Some useful screenshots: enter image description here enter image description here

+4
Oct. 19 '17 at 20:21
source share



All Articles