Используйте with_traceback
import sys, traceback
try:
10/0
except Exception as exc:
raise exc.with_traceback(None)
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-31-d77f0aded0d7> in <module>()
3 10/0
4 except Exception as exc:
----> 5 raise exc.with_traceback(None)
ZeroDivisionError: division by zero
Если вы просто хотите показать это:
import sys, traceback
try:
10/0
except Exception:
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
File "<ipython-input-4-1283199eb169>", line 3, in <module>
10/0
ALTERNATIVE
import sys, traceback
try:
10/0
except Exception as exc:
tb_str = traceback.format_exception(etype=type(exc), value=exc, tb=exc.__traceback__)
for i in tb_str: print(i)
Traceback (most recent call last):
File "<ipython-input-17-3bc95dc2ebf5>", line 3, in <module>
10/0
ZeroDivisionError: division by zero