1.3.2.1 Post-Mortem Debugging within a Python Interpreter Shell (pydb.post_mortem, pydb.pm())

Here, all you do is import pydb (if that hasn't been done already), and call pydb.pm() or pydb.post_mortem if you have a specific traceback object you want to use.

To make this more concrete we will give an example. We have the following text mymodule.py

def test():
    print spam

Now here's a sample session

>>> import mymodule
>>> mymodule.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "mymodule.py", line 2, in test
    print spam
NameError: global name 'spam' is not defined
>>> import pydb
>>> pydb.pm()
(/home/src/external-cvs/pydb/test/mymodule.py:2):  test
(Pydb) where
-> 0 test() called from file '/tmp/mymodule.py' at line 2
## 1 in file '<stdin>' at line 1
(Pydb) list
  1     def test():
  2  ->     print spam
[EOF]
(Pydb) quit
>>>

At present if you are using ipython, that captures the exception and sys.last_traceback will not be defined.

If you have a traceback stored say in variable t, instead of pydb.pm() above, use pydb.post_mortem(t).

See About this document... for information on suggesting changes.