Python debugger: Stepping into a function that you have called interactively Python debugger: Stepping into a function that you have called interactively python python

Python debugger: Stepping into a function that you have called interactively


And I've answered my own question! It's the "debug" command in pydb:

~> cat -n /tmp/test_python.py     1  #!/usr/local/bin/python     2     3  def foo():     4      print "hi"     5      print "bye"     6     7  exit(0)     8~> pydb /tmp/test_python.py(/tmp/test_python.py:7):  <module>7 exit(0)(Pydb) debug foo()ENTERING RECURSIVE DEBUGGER------------------------Call level 11(/tmp/test_python.py:3):  foo3 def foo():((Pydb)) s(/tmp/test_python.py:4):  foo4     print "hi"((Pydb)) shi(/tmp/test_python.py:5):  foo5     print "bye"((Pydb)) sbye------------------------Return from level 11 (<type 'NoneType'>)----------------------Return from level 10 (<type 'NoneType'>)LEAVING RECURSIVE DEBUGGER(/tmp/test_python.py:7):  <module>


You can interactively debug a function with pdb as well, provided the script you want to debug does not exit() at the end:

$ cat test.py#!/usr/bin/pythondef foo(f, g):        h = f+g        print h        return 2*f

To debug, start an interactive python session and import pdb:

$ pythonPython 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import pdb>>> import test>>> pdb.runcall(test.foo, 1, 2)> /Users/simon/Desktop/test.py(4)foo()-> h = f+g(Pdb) n> /Users/simon/Desktop/test.py(5)foo()-> print h(Pdb) 

The pdb module comes with python and is documented in the modules docs at http://docs.python.org/modindex.html


There is a python debugger that is part of the core distribution of python called 'pdb'. I rarely use it myself, but find it useful sometimes.

Given this program:

def foo():    a = 0    print "hi"    a += 1    print "bye"foo()

Here is a session debugging it:

$ python /usr/lib/python2.5/pdb.py /var/tmp/pdbtest.py         ~> /var/tmp/pdbtest.py(2)<module>()-> def foo():(Pdb) s> /var/tmp/pdbtest.py(10)<module>()-> foo()(Pdb) s--Call--> /var/tmp/pdbtest.py(2)foo()-> def foo():(Pdb) s> /var/tmp/pdbtest.py(3)foo()-> a = 0(Pdb) s> /var/tmp/pdbtest.py(4)foo()-> print "hi"(Pdb) print a0(Pdb) shi> /var/tmp/pdbtest.py(6)foo()-> a += 1(Pdb) s> /var/tmp/pdbtest.py(8)foo()-> print "bye"(Pdb) print a1(Pdb) sbye--Return--> /var/tmp/pdbtest.py(8)foo()->None-> print "bye"(Pdb) s--Return--> /var/tmp/pdbtest.py(10)<module>()->None-> foo()(Pdb) s