Is there a way to show only important directory paths when running a python program?
I am currently getting the following:
python3 foo.py Traceback (most recent call last): File "foo.py", line 60, in <module> foo = Foo() File "foo.py", line 22, in __init__ self._run() File "/media/MyDocuments/xxxxxxx/yyyyyyyyy/python_code/foo.py", line 18, in check_input bar = obj.get_action() AttributeError: 'obj' object has no attribute 'get_action'
As I know in which directory my code is located, the full directory makes the error message less readable. Can I tell python to show me the result more than this?
python3 foo.py Traceback (most recent call last): File "foo.py", line 60, in <module> foo = Foo() File "foo.py", line 22, in __init__ self._run() File ".../foo.py", line 18, in check_input bar = obj.get_action() AttributeError: 'obj' object has no attribute 'get_action'
Answer
Using the code from unutbu , I added some lines for the colors, if someone is looking for a slight improvement on the interpreter output, just use this as a module and import it:
import sys import traceback import os import re RED = '\033[91m' GREEN = '\033[92m' YELLOW = '\033[93m' LIGHT_PURPLE = '\033[94m' PURPLE = '\033[95m' CYAN = '\033[96m' END = '\033[0m' def my_excepthook(type, value, tb): lines = traceback.format_list(traceback.extract_tb(tb)) def shorten(match): return 'File "{}"'.format(os.path.basename(match.group(1))) lines = [re.sub(r'File "([^"]+)"', shorten, line) for line in lines] _print_color(lines)