Python debugging

Python debugger
Python debugger

The Python debugger (PDB) is a python module that will give you similar functionality to GDB for python code. There are 2 ways you can run PDB:

/usr/local/rnt/bin/python -m pdb [OPTIONS]

python -m pdb
> /root/python/
-> import os
(Pdb) # you need to enter the word "continue" or see the "Useful Commands:"a few lines down for more commands.
hello world
The program finished and will be restarted


Python 2.4.4 (#1, Feb 6 2009, 15:33:11)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> import hmsconstants
>>> import datalib
>>>'x = datalib.Site("abc")')
> (1)?()

The first method is good to debug a whole script. The second is for individual functions.

Useful Commands:

r - This will cause the current function to return.
c - similar to r but this option will stop at break points.
s - This will let you step through execution one line at a time, stepping into each function that is called.
n - similar to s but this option will not go into each function.
p - p takes one argument which it will print out
pp - Just like p but it will use pprint to display the argument
b - Set a break point. b takes arguments that can be specifed as 'file:line#' or 'function name'
tbreak - Same as b but the break point will only work the first time its hit

l will show where you are in the program
pressing p var1 # so p 'variable' will show value of variable at the current point
!var1 = "R"  # will change the value of  variable var to R

Full documentation is here: also see a good tutorial from

in your script if you add:
import pdb
# will drop into debug mode when hit

from bash pydoc -k num & then pydoc numbers or pip search pdf

python help

>>> type (2)

dir(a) # based on what dir thinks is in a it will give you a help sensitive context
>>> dir(2)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

>>> help (a)
Help on int object:

class int(object)
| int(x[, base]) -> integer
| Convert a string or number to an integer, if possible. A floating point
| argument will be truncated towards zero (this does not include a string
| representation of a floating point number!) When converting a string, use
| the optional base. It is an error to supply a base when converting a
| non-string. If base is zero, the proper base is guessed based on the
| string content. If the argument is outside the integer range a
| long object will be returned instead

another example:
import os

help("modules") # will show all avalliable modules
or mylist=[] & help(mylist)