這裏就是你需要
http://ynniv.com/blog/2007/11/debugging-python.html
三種方式,第一種是簡單的,但原油(Thomas Heller) - 以下內容添加到站點包/ sitecustomize.py:
import pdb, sys, traceback
def info(type, value, tb):
traceback.print_exception(type, value, tb)
pdb.pm()
sys.excepthook = info
的其次是更復雜,並檢查交互模式(奇怪地跳過交互模式下的調試),從cookbook:
# code snippet, to be included in 'sitecustomize.py'
import sys
def info(type, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type, value, tb)
else:
import traceback, pdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type, value, tb)
print
# ...then start the debugger in post-mortem mode.
pdb.pm()
sys.excepthook = info
而第三個通過(總是啓動調試器,除非標準輸入或標準錯誤都被重定向)ynniv
# code snippet, to be included in 'sitecustomize.py'
import sys
def info(type, value, tb):
if (#hasattr(sys, "ps1") or
not sys.stderr.isatty() or
not sys.stdin.isatty()):
# stdin or stderr is redirected, just do the normal thing
original_hook(type, value, tb)
else:
# a terminal is attached and stderr is not redirected, debug
import traceback, pdb
traceback.print_exception(type, value, tb)
print
pdb.pm()
#traceback.print_stack()
original_hook = sys.excepthook
if sys.excepthook == sys.__excepthook__:
# if someone already patched excepthook, let them win
sys.excepthook = info
正是我想要的,謝謝。 – saffsd 2009-08-06 07:52:20