How to get stack trace string without raising exception in python? How to get stack trace string without raising exception in python? python python

How to get stack trace string without raising exception in python?


It's traceback.extract_stack() if you want convenient access to module and function names and line numbers, or ''.join(traceback.format_stack()) if you just want a string that looks like the traceback.print_stack() output.


Use the inspect module. In particular, inspect. currentframe() -- but the whole module is there for the purpose of looking at the state of your program at a given time.

Some useful inspect tricks here.