Is there a way to show only the important directory paths when executing a python program?
Currently I get 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 "/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, the full directory makes the error message just worse readable. Can I tell python to show me the output more like 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 colors, in case someone is looking for an easy improvement of 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)
# print(''.join(lines))
print(RED + '{}: {}'.format(type.__name__, value) + END)
sys.excepthook = my_excepthook
def _print_color(lines):
for l in lines:
for i in range(len(l)-1):
if l[i:i+5]=="line ":
i +=5
# Find the length of the number
numLen = 0
while l[i+numLen].isdigit():
numLen +=1
# Find the length of the function
funLen = 0
while not l[i+numLen+4 + funLen]=="\n":
funLen+=1
l = ''.join([l[:i],
YELLOW+"{}".format(l[i:i+numLen])+END,
l[i+numLen:i+numLen+5],
LIGHT_PURPLE+"{}".format(l[i+numLen+5:i+numLen+5+funLen])+END,
CYAN+"{}".format(l[i+numLen+5+funLen:])+END])
print(l,end="")
break
print("")