Color Python errors (stack traces) to make them easier to understand

Use Pygments to make the stack trace a little more beautiful.

sample_code.py


def funcA():
  funcB()

def funcB():
  funcC()

def fucnC():
  1/0  # error!

funcA()

↓ It looks like this when it is raw

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    funcA()
  File "test.py", line 2, in funcA
    funcB()
  File "test.py", line 5, in funcB
    funcC()
NameError: name 'funcC' is not defined

↓ I want to make it as beautiful as this

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    funcA()
  File "test.py", line 2, in funcA
    funcB()
  File "test.py", line 5, in funcB
    funcC()
NameError: name 'funcC' is not defined

1. Install Pygments

Whether it's pip, ʻeasy_install, or port`.

pip install Pygments

2. Add the following to the beginning of your Python code

Use sys.excepthook.

coloring.py


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

Reference: Coloring exceptions from Python on a terminal

For the time being, the character string and numerical value of the stack trace output by this will be colored.

(Development) 3. Automatically execute the above code when executing Python code

It's a hassle to add the above to all the code, so let's do it for yourself. _ (It can lead to bugs that don't make sense, so it's a good idea to understand what you're doing before trying.) _

Use ʻuser customize`. Reference: 16.1.4. Modules for customization

Create ʻusercustomize.py in the directory output by python -c "import site; print (site.getusersitepackages ())" and write the above code coloring.py` and you're done.

Impressions

The feeling of trying it and the ease of understanding do not change much! !!

Recommended Posts

Color Python errors (stack traces) to make them easier to understand
Make the display of Python module exceptions easier to understand
Tips to make Python here-documents easier to read
Try to understand Python self
You who color the log to make it easier to see
Join csv normalized by Python pandas to make it easier to check
Note: Improving annoying prompts to make them easier to read. // Linux prompt change
Make SikuliX's click function easier to use
[Python] How to make a class iterable
Output color characters to pretty with python
Fractal to make and play with Python
Make pypy submission easier with atcoder-cli (python)
[Python] Pandas to fully understand in 10 minutes
10 Python errors that are common to beginners
[Python] Understand how to use recursive functions
[Python] How to deal with module errors
A way to understand Python duck typing