Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to backtrace a function in python 2.7?

I have a big python script, with multiple files, and I need to know where a method was called. Is there a backtrace function in python like debug_backtrace in php?

like image 974
mimrock Avatar asked Apr 26 '12 08:04

mimrock


People also ask

How do you use TB Python?

print_tb(tb, limit = None, file = None) : If limit is positive it prints upto limit stack trace entries from traceback object tb. Otherwise, print the last abs(limit) entries. If limit is omitted or None, all entries are printed. If file is omitted or None, the output goes to sys.

How do I fix Python traceback error?

The traceback error also shows the type of error and information about that error. The above case is IndexError: list index out of range . You can fix it using the valid index number to retrieve an item from a list.

Does Python have a call stack?

The Python interpreter uses a call stack to run a Python program. When a function is called in Python, a new frame is pushed onto the call stack for its local execution, and every time a function call returns, its frame is popped off the call stack.


1 Answers

See the traceback module.

import traceback

def foo():
    bar()

def bar():
    baz()

def baz():
    traceback.print_stack() 
    # or trace = traceback.extract_stack()

foo()
like image 153
georg Avatar answered Sep 30 '22 02:09

georg