Traceback module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter.
traceback.print_exc([limit[, file]])
This is a shorthand for print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file).
Print exception information and up to limit stack trace entries from the traceback tb to file. If limit is omitted or None, all entries are printed. If file is omitted or None, the output goes to sys.stderr.
Demo
This simple example implements a basic read-eval-print loop, similar to (but less useful than) the standard Python interactive interpreter loop.
import sys, traceback
def run_user_code(envdir):
source = raw_input(">>> ")
try:
exec source in envdir
except:
print "Exception in user code:"
print '-'*60
traceback.print_exc(file=sys.stdout)
print '-'*60
envdir = {}
while 1:
run_user_code(envdir)
When we run it as follows:
$ python demo.py
>>> 1a
Exception in user code:
------------------------------------------------------------
Traceback (most recent call last):
File "demo.py", line 8, in run_user_code
exec source in envdir
File "<string>", line 1
1a
^
SyntaxError: invalid syntax
------------------------------------------------------------
We get the exception information just like as we use interactive interpreter.
This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed.
In all cases, if the optional parts are omitted, the code is executed in the current scope.
>>> a = 2
>>> exec "a = 1"
>>> a
1
If only the first expression after in is specified, it should be a dictionary, which will be used for both the global and the local variables.
>>> a = 10
>>> b = 20
>>> g = {'a': 6, 'b': 8}
>>> exec "global a; print a, b" in g
6 8
If two expressions are given, both must be dictionaries and they are used for the global and local variables, respectively.
>>> a = 10
>>> b = 20
>>> c = 20
>>> g = {'a': 6, 'b': 8}
>>> l = {'b': 9, 'c':10}
>>> exec "global a; print a,b,c" in g, l
6 9 10
Traceback module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter.
print_exception is here:
Demo
This simple example implements a basic read-eval-print loop, similar to (but less useful than) the standard Python interactive interpreter loop.
When we run it as follows:
We get the exception information just like as we use interactive interpreter.
exec
This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed.
In all cases, if the optional parts are omitted, the code is executed in the current scope.
If only the first expression after in is specified, it should be a dictionary, which will be used for both the global and the local variables.
If two expressions are given, both must be dictionaries and they are used for the global and local variables, respectively.
Ref
traceback — Print or retrieve a stack traceback
The exec statement
python exec and eval