xxxxxxxxxx
## Preliminaries
β
Make sure that you have saved the function below to a script called debugme.py. This script should be in the current working directory for your notebook.
Type Markdown and LaTeX:
x
def createabug(x):
y = x**4
z = 0.
y = y/z
return y
β
createabug(5)
run debugme.py
## the python debugger
β
We start the python debugger in the jupyter notebook by typing pdb
β
We start the python debugger in the jupyter notebook by typing pdb
%pdb
β
Notice what happens when we run the script now.
Notice what happens when we run the script now.
run debugme.py
β
Call pdb a second time to turn it off.
Call pdb a second time to turn it off.
%pdb
## 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
β
β
β
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.
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
x
β
Now start the debugger in your notebook and run debugme.py again:
xxxxxxxxxx
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()
```
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
β
```python
β
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`
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
ls
β
run pdb_debugme.py
β