I do not know if there is a flag to do this, but if you really want to, you can override sys.excepthook with your own function, within which you can create a TracebackException, remove all filenames from the frame summaries, and format and print it.
import os
import sys
import traceback
def handler(_exception_type, _value, t):
exc = traceback.TracebackException(_exception_type, _value, t)
# replace file names for each frame summary
for frame_summary in exc.stack:
frame_summary.filename = os.path.relpath(frame_summary.filename)
# format and print the exception
print(''.join(exc.format()), file=sys.stderr)
sys.excepthook = handler
def crashes_hard():
print(1 / 0)
def crashes():
crashes_hard()
crashes()
The output is
Traceback (most recent call last):
File "scratch_1.py", line 31, in <module>
crashes()
File "scratch_1.py", line 28, in crashes
crashes_hard()
File "scratch_1.py", line 24, in crashes_hard
print(1 / 0)
ZeroDivisionError: division by zero
The original output is
Traceback (most recent call last):
File "/home/abhijat/.config/.../scratches/scratch_1.py", line 31, in <module>
crashes()
File "/home/abhijat/.config/.../scratches/scratch_1.py", line 28, in crashes
crashes_hard()
File "/home/abhijat/.config/.../scratches/scratch_1.py", line 24, in crashes_hard
print(1 / 0)
ZeroDivisionError: division by zero