Type Markdown and LaTeX: Ξ±2

In [1]:
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-1-c2ec56e9f29b> in <module>
      5     return y
      6 
----> 7 createabug(5)

<ipython-input-1-c2ec56e9f29b> in createabug(x)
      2     y = x**4
      3     z = 0.
----> 4     y = y/z
      5     return y
      6 

ZeroDivisionError: float division by zero

In [2]:
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
~/Dropbox/CSB/unix/sandbox/debugme.py in <module>
      5     return y
      6 
----> 7 createabug(5)

~/Dropbox/CSB/unix/sandbox/debugme.py in createabug(x)
      2     y = x**4
      3     z = 0.
----> 4     y = y/z
      5     return y
      6 

ZeroDivisionError: float division by zero

the python debuggerΒΆ

We start the python debugger in the jupyter notebook by typing pdb

In [3]:
Automatic pdb calling has been turned ON

Notice what happens when we run the script now.

In [4]:
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
~/Dropbox/CSB/unix/sandbox/debugme.py in <module>
      5     return y
      6 
----> 7 createabug(5)

~/Dropbox/CSB/unix/sandbox/debugme.py in createabug(x)
      2     y = x**4
      3     z = 0.
----> 4     y = y/z
      5     return y
      6 

ZeroDivisionError: float division by zero

> /Users/michaelalfaro/Dropbox/CSB/unix/sandbox/debugme.py(4)createabug()
      2     y = x**4
      3     z = 0.
----> 4     y = y/z
      5     return y
      6 

ipdb> x
5
ipdb> y
625
ipdb> z
0.0
ipdb> y/z
*** ZeroDivisionError: float division by zero
ipdb> q

Call pdb a second time to turn it off.

In [5]:
Automatic pdb calling has been turned OFF

Working within the debuggerΒΆ

The ipdb prompt takes commands that allow us to walk through a program and to examine variables.

This can help clarify why a program is behaving improperly.

ipdb commandsΒΆ

Within the debugger we can move around in our code and examine variables to see what is happening.

b move to the next line.
ENTER repeat the previous command.
s β€œstep” into function or procedure (i.e., continue the debugging inside the function, as opposed to simply run it). p x print variable x.
c continue until next break-point.
q quit
l print the code surrounding the current position.
r continue until the end of the function.

T

Now start the debugger in your notebook and run debugme.py again:

If you want to start pdb within a longer script, place this line at the point you want to examine:

import pdb; 
pdb.set_trace()

so modify your earlier script like so


import pdb

def createabug(x):
    y = x**4
    z = 0.
    pdb.set_trace() # will start debugger 
    y = y/z
    return y

createabug(5)

And try running it. The debugger will start at the point where you set the trace (just before y = y/z

In [7]:
Thursday 2-6.ipynb             filelist.txt
debugme.py                     pdb_debugme.py
dnaseq.txt                     python debugger example.ipynb
file_input_output.ipynb
In [*]:
> /Users/michaelalfaro/Dropbox/CSB/unix/sandbox/pdb_debugme.py(7)createabug()
-> y = y/z
(Pdb) x
5
(Pdb) y
625
(Pdb) z
0.0
(Pdb) 
In [ ]: