On this page
exec
Prev: eval | Up: __builtins__ | Next: exit
Rel: breakpoint, compile, eval, globals, input
Ref: docs.python.org — exec
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import exec
Overview & Signature
exec(source, /, globals=None, locals=None, *, closure=None)
Execute the given source in the context of globals and locals.
Usage
ns: dict[str, object] = {}
exec("x = 6 * 7", ns)
assert ns["x"] == 42
Gotchas
- Returns
Noneand is NOT a sandbox: Always pass an explicit namespace dictnsrather than relying onlocals()inside a function.
REPL Inspect
help(exec)
Linked from
- tuts/python3/__builtins__
- tuts/python3/__builtins__/__builtins__
- tuts/python3/__builtins__/breakpoint
- tuts/python3/__builtins__/compile
- tuts/python3/__builtins__/eval
- tuts/python3/__builtins__/exit
- tuts/python3/__builtins__/globals
- tuts/python3/__builtins__/input
- tuts/python3/__builtins__/locals
- tuts/python3/__builtins__/open
- tuts/python3/__builtins__/print
- tuts/python3/python metaprogramming
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/breakpoint Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/compile Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/eval Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/exit Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/globals Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/input Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/locals links to this page; shared tags: #builtins
- tuts/python3/__builtins__/open links to this page; shared tags: #builtins