How to catch error from another file and get the method and the error string?

I have the files:
main.py
test_module.py

main.py:
import sys, os
import test_module
try:
test_module.test_method_1()
except Exception as err:
 exc_type, exc_obj, exc_tb = sys.exc_info()
 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
 message = '{}:{} - {}'.format(fname, exc_tb.tb_lineno, err)
 print(message)


test_module.py:
def test_method_1():
 1/0


The code prints:
main.py:4 - division by zero

I would like to deduce that an error has occurred in the file test_module.py
What about this:
test_module.py:2 - division by zero
March 23rd 20 at 18:55
2 answers
March 23rd 20 at 18:57
Solution
March 23rd 20 at 18:59
import traceback

print(traceback.format_exc())

print in the right except block

Find more questions by tags Python