Coloring exceptions from Python on a terminal

后端 未结 4 885
天命终不由人
天命终不由人 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:13

    Have a look at colorama ( or any other coloring ) module. Then you can wrap you're entire app with:

    import traceback
    from colorama import Fore, init
    init( )
    
    try:
        // your app
    except Exception:
        print Fore.RED + traceback.format_exc( ) + Fore.RESET
        // possibly raise again or log to db
    

提交回复
热议问题