Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

traceback.format_exc()

can get it with raising an exception.

traceback.print_stack()

prints the stack without an exception needed, but it does not return a string.

There doesn't seem to be a way to get the stack trace string without raising an exception in python?

like image 550
user299648 Avatar asked Aug 05 '13 01:08

user299648


People also ask

How do I print a stack trace in Python except?

Method 1: By using print_exc() method. This method prints exception information and stack trace entries from traceback object tb to file.

How do I print a stack trace for exceptions?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.


2 Answers

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.

like image 62
user2357112 supports Monica Avatar answered Oct 22 '22 21:10

user2357112 supports Monica


How about traceback.format_stack?

like image 31
Stobor Avatar answered Oct 22 '22 20:10

Stobor