How do I print an exception in Python?

You can print an exception in Python by using the print() function and passing the exc variable which is the default variable name for an exception.

For example:

try:
    i = j # j is not defined
except Exception as exc:
    print(exc)

Watch a course Python - The Practical Guide

You can also use the traceback module to print a more detailed traceback of the exception.

import traceback
try:
    # some code that might raise an exception
except Exception:
    traceback.print_exc()

You can also use traceback.print_tb(e.__traceback__) to print the traceback of a specific exception instance.

import traceback
try:
    # some code that might raise an exception
except Exception as e:
    traceback.print_tb(e.__traceback__)