So I have a 2 files.
test.py
import sys
import error_handler
sys.excepthook = error_handler;
value = 23/0; #this line will throw Zero division error
error_handler.py
def custom_error_handler(ex_class, ex, tb):
fileName = os.path.split(tb.tb_frame.f_code.co_filename)[1];
lineNo = tb.tb_lineno;
print fileName;
print lineNo;
These are the outputs when I run the file and when I run it as a module.
python test.py
test.py
4
But if I run
python -m test
runpy.py
162
Any ideas on why running it the second way produces that result? is there another way I should be doing this?
Thankyou.
ok I solved this by following viraptors comment and the python documentation .
I had to import traceback, split the traceback into an array
import traceback;
tb_array = traceback.extract_tb(tb);
each element in the array is an array that contains:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.