Apr 27, 2019

How to enable line-by-line python debugging in jupyter + Anaconda environment or Google Colab environment?

First, Anaconda has the default python version running at 3.7 but we will need 3.6




First trying out this jupyter program:

https://pytorch.org/tutorials/beginner/nn_tutorial.html

or as captured here:

https://gist.github.com/tthtlc/42e68c773988437c1a4158d55f26f819

First look for the "set_trace()" command inside here:


This is where you enable the jupyter debugging.

If you are in a Google Colab environment, you just need to insert:

from IPython.core.debugger import set_trace
set_trace()

(for example see this:


After running "set_trace()" ipython debugger will stop at the next line and you can enter "help":

ipdb> help

Documented commands (type help ):
========================================
EOF    cl         disable  interact  next    psource  rv         unt   
a      clear      display  j         p       q        s          until 
alias  commands   down     jump      pdef    quit     source     up    
args   condition  enable   l         pdoc    r        step       w     
b      cont       exit     list      pfile   restart  tbreak     whatis
break  continue   h        ll        pinfo   return   u          where 
bt     d          help     longlist  pinfo2  retval   unalias  
c      debug      ignore   n         pp      run      undisplay

Miscellaneous help topics:
==========================
exec  pdb
Now entering "n" to continue execution:
ipdb> n
> d26dbfac9245>(16)()
     14         set_trace()
     15         start_i = i * bs
---> 16         end_i = start_i + bs
     17         xb = x_train[start_i:end_i].to(dev)
     18         yb = y_train[start_i:end_i].to(dev)

ipdb> 
> d26dbfac9245>(17)()
     15         start_i = i * bs
     16         end_i = start_i + bs
---> 17         xb = x_train[start_i:end_i].to(dev)
     18         yb = y_train[start_i:end_i].to(dev)
     19         pred = model(xb)

ipdb> 
> d26dbfac9245>(18)()
     16         end_i = start_i + bs
     17         xb = x_train[start_i:end_i].to(dev)
---> 18         yb = y_train[start_i:end_i].to(dev)
     19         pred = model(xb)
     20         loss = loss_func(pred, yb)


No comments: