Next / Previous / Contents / Shipman's homepage

22.4. The exec statement: Execute Python source code

To dynamically execute Python code, use a statement of this form:

exec E0 [in E1 [, E2]]

Expression E0 specifies what to execute, and may be a string containing Python source code, an open file, or a code object. If E1 is omitted, the code is executed in the local scope. If E1 is given but E2 is not, E1 is a dictionary used to define the names in the global and local scopes. If E2 is given, E1 is a dictionary defining the global scope, and E2 is a dictionary defining the local scope.