all Python 3tracebackfunction
traceback.print_stack()
printing tracebacks
Prints the current call stack to stderr — a quick answer to "how did the program get here" without a debugger.
Syntax
traceback.print_stack(f=None, limit=None, file=None)
No exception is needed: the call can go anywhere in the code. Output goes to stderr while print goes to stdout, and when both are redirected to a file their lines may come out of order: stdout is buffered, stderr is not. Pass file=sys.stdout to keep the order. limit=N prints only the N nearest frames.
Examples
import sys, traceback
def handler():
traceback.print_stack(limit=1, file=sys.stdout)
handler() # → ? File "…/example.py", line 3, in handler — путь зависит от машины
See also
Open in the full glossarydocs.python.orgReport an inaccuracy