e.printStackTrace equivalent in python

koool picture koool · Mar 4, 2012 · Viewed 130.3k times · Source

I know that print(e) (where e is an Exception) prints the occurred exception but, I was trying to find the python equivalent of Java's e.printStackTrace() that exactly traces the exception to what line it occurred and prints the entire trace of it.

Could anyone please tell me the equivalent of e.printStackTrace() in Python?

Answer

ThiefMaster picture ThiefMaster · Mar 4, 2012
import traceback
traceback.print_exc()

When doing this inside an except ...: block it will automatically use the current exception. See http://docs.python.org/library/traceback.html for more information.