In Python 2, exec
is a statement, with special syntax: exec code [in globals[, locals]].
In Python 3 exec
is now a function: exec(code, [, globals[, locals]])
, and the Python 2 syntax will raise a SyntaxError
.
As print
was changed from statement into a function, a __future__
import was also added. However, there is no from __future__ import exec_function
, as it is not needed: the exec statement in Python 2 can be also used with syntax that looks exactly like the exec
function invocation in Python 3. Thus you can change the statements
exec 'code'
exec 'code' in global_vars
exec 'code' in global_vars, local_vars
to forms
exec('code')
exec('code', global_vars)
exec('code', global_vars, local_vars)
and the latter forms are guaranteed to work identically in both Python 2 and Python 3.