Is there any way in Python to stop program execution in the middle of the code?
Many programs written by scientists are not complex enough to
require the use of a debugger. Many of us prefer to check our
code by putting in stop
statements and printing
out the values of selected variables.
Python does not have a stop
statement.
I looked really really hard for one. There is a break
statement for getting out of loops, and there's an elegant
exception handler, but nothing exists in the language to just
stop program execution to let you inspect the state of the
program.
Thankfully, through one of the methods in the Pdb
debugger (included with the standard Python distribution),
we can simulate a stop
statement. If
we are executing code in an interpreter window, the program
will even stop execution and allow us to inspect any variable
we want, like in IDL.
I learned how to do this from Stephen Berg's excellent article
Debugging in Python. Here I'll summarize how to
use Pdb
to stop program execution.
Say we have the following simple program:
a = 1
b = a+2
print 'Line 3'
print 'Line 4'
and we wish to put a "stop" after b = a+2
,
like this:
a = 1
stop
b = a+2
print 'Line 3'
print 'Line 4'
To do this, I add the following snippet of code at the "stop":
import pdb #@@@
pdb.set_trace() #@@@
print 'stop here' #@@@
The @@@
comments are there so I can easily find those
lines to remove them after I've finished debugging.
So, the program now reads:
a = 1
b = a+2
import pdb #@@@
pdb.set_trace() #@@@
print 'stop here' #@@@
print 'Line 3'
print 'Line 4'
When you run the program, after b = a+2
executes, you will be put into the Pdb debugger. Your
Python session (I'm assuming you're using the interactive
interpreter) will look something like this:
>>> execfile('foo.py')
--Return--
> /usr/lib/python2.1/pdb.py(895)set_trace()->None
-> Pdb().set_trace()
(Pdb)
Pdb has this interesting feature in that when you first invoke it
(via the pdb.set_trace()
call), it enters the "Pdb"
mode but doesn't do or know anything. For instance, if you try
to inspect a program variable, it doesn't know the variable
exists.
To see everything in your program, you have to step forward one
line, by typing n
at the (Pdb)
prompt.
You'll now get something like:
(Pdb) n
> /home/jlin/foo.py(5)?()
-> print 'stop here' #@@@
(Pdb)
At this point, you can print whatever is currently defined in
your program.
In fact, you can now execute any interactive
Python statement (except definitions like loops, etc.)
that you want at the (Pdb)
prompt. There are
some exceptions; sometimes, you have to add an exclamation point
(!) to the statement in order for it to work. See
Debugging in Python for details.
So, if you're interested in seeing the value of b
,
at the (Pdb)
prompt type:
(Pdb) !b
3
(Pdb)
and the value of b
is returned. Note that b
without the !
means "set breakpoint" in Pdb. We add the
!
so Pdb knows to return the value of variable b
,
not to set a breakpoint.
When you're done, press q
to get out of
Pdb. You'll get a bunch of traceback messages, before you
return to the interactive Python prompt (>>>
),
but just ignore those messages.