Python Reference ยท Built-in Functions
Python compile() function
Reference for Python compile(): turn source text into a code object for exec() or eval(), the exec, eval and single modes, syntax errors and security warnings.
The built-in compile() function turns source code, given as a string, bytes or AST object, into a code object. A code object can be run with exec() or eval() as many times as you like without parsing the text again.
Most programs never need it. It matters for tools that run user-supplied snippets repeatedly, such as templating engines, REPLs, test frameworks and plugin systems. Never compile and run code from sources you do not trust.
code = compile("1 + 2", "<string>", "eval") eval(code) # 3
Try it
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.
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
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
| Name | Description |
|---|---|
source |
A str, bytes or ast.AST object. |
filename |
The name shown in tracebacks. Use "<string>" when the code does not come from a file. |
mode |
"exec" for a module-like block of statements, "eval" for a single expression, "single" for one interactive statement. |
flags |
Optional compiler flags, such as ast.PyCF_ONLY_AST to get a syntax tree instead of a code object. |
dont_inherit |
If true, ignore from __future__ statements active in the calling code. |
optimize |
Optimization level: -1 (the interpreter's level), 0, 1 or 2. |
Return value: a code object (or an ast.AST object with ast.PyCF_ONLY_AST).
Raises: SyntaxError for invalid code, ValueError if the source contains null bytes or the mode is invalid.
Usage notes
- Running it.
exec(code, globals_dict)runs statements and returnsNone;eval(code, globals_dict)returns the expression's value. An"exec"code object evaluated witheval()runs but returnsNone. - Mode must match.
"eval"rejects statements such as assignments, and"exec"is the mode for anything with more than one statement. - Namespaces. Always pass your own dictionary to
exec/eval; otherwise the code uses the caller's globals and can overwrite them. - Not a sandbox. Passing
{"__builtins__": {}}does not make untrusted code safe. Avoid executing code from users. - Errors carry position. The
SyntaxErrorhaslineno,offset,filenameandtext, which are handy for showing helpful messages. - Reading code objects. Attributes such as
co_names,co_constsandco_filenameshow what the compiler found. Thedismodule prints the bytecode. - Source files. Python caches compiled modules in
__pycache__automatically; you rarely compile files yourself. - Safer for data. For literals only,
ast.literal_eval()parses without running code.