Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Built-in Functions

Python exec() function

Reference for Python exec(): run Python statements from a string or code object, control the namespace, read results back and understand the security risks.

exec() runs Python code that is held in a string (or a code object). Unlike eval(), it accepts full statements: assignments, function and class definitions, loops, imports. It always returns None; results are communicated through the namespace you give it.

Because the code can do anything your program can do, never pass text from untrusted sources to exec(). It is mostly useful for tools such as REPLs, templating or plugin systems, where running code is the point. For ordinary configuration or data, use files in a data format instead.

Python
exec("total = 1 + 2")
print(total)
# 3

Try it

Python Running statements in a namespace

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Globals, locals and scope surprises

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
exec(source, globals=None, locals=None, *, closure=None)
Name Description
source A string or bytes of Python code, or a code object from compile(). A string may contain several statements.
globals Optional dictionary for the global namespace. Defaults to the caller's globals.
locals Optional mapping for the local namespace. Defaults to globals if only that is given.
closure Optional tuple of cells, only valid with a code object that has free variables (Python 3.11+).

Return value: always None.

Raises: SyntaxError for bad code; whatever exception the executed code raises.

Usage notes

  • Same risks as eval(), with more power: it can define functions, import modules and change files. Do not run untrusted code, and do not treat an emptied __builtins__ as a sandbox.
  • Read results from the namespace. Pass a dictionary and look at it afterwards, as in the first example.
  • Two namespaces behave like a class body. If you give separate globals and locals, top-level code runs as if inside a class: names assigned there are not visible inside functions defined by the same code. Pass a single dictionary to avoid this.
  • Without namespaces the code runs in the caller's scope. Inside a function, changes to local variables are not reliably visible afterwards, because function locals are optimized.
  • Be explicit. Whatever the Python version, pass your own dictionary and read results from it rather than depending on how locals() behaves in the caller.
  • Compile once with compile(source, name, "exec") when running the same code many times; the name shows up in tracebacks.
  • Indentation. A multi-line string must have consistent indentation. Use textwrap.dedent() for indented triple-quoted blocks.