import sys

def myexcepthook(type, value, tb):
    import traceback
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import TerminalFormatter
tbtext = ''.join(traceback.format_exception(type, value, tb))
lexer = get_lexer_by_name('pytb', stripall=True)
formatter = TerminalFormatter()
sys.stderr.write(highlight(tbtext, lexer, formatter))
sys.excepthook = myexcepthook
Coloring exceptions from Python on a terminal