Coloring exceptions from Python on a terminal

后端 未结 4 902
天命终不由人
天命终不由人 2021-02-19 14:02

Is there an easy way to get the message of the exception to be colored on the command line? For example

def g():    f()
def f():    1/0
g()
4条回答
  •  臣服心动
    2021-02-19 14:32

    Found another way to do this using the IPython module which is likely a dependency that everyone already has installed:

    from IPython.core.ultratb import ColorTB
    c = ColorTB()
    exc = sys.exc_info()
    print(''.join(c.structured_traceback(*exc)))
    

提交回复
热议问题