PyDocs

Nav

Python Runtime Services

compile

builtins

function
  • compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1, *, module=None)
Compile source into a code object that can be executed by exec() or eval().

The source code may represent a Python module, statement or
expression.
The filename will be used for run-time error messages.
The mode must be 'exec' to compile a module, 'single' to compile a
single (interactive) statement, or 'eval' to compile an expression.
The flags argument, if present, controls which future statements
influence the compilation of the code.
The dont_inherit argument, if true, stops the compilation inheriting
the effects of any future statements in effect in the code calling
compile; if absent or false these statements do influence the
compilation, in addition to any features explicitly specified.
Changed in 3.2, 3.5, 3.8, 3.12, 3.15
  • 3.2: Allowed use of Windows and Mac newlines. Also, input in 'exec' mode does not have to end in a newline anymore. Added the optimize parameter.
  • 3.5: Previously, TypeError was raised when null bytes were encountered in source.
  • 3.8: ast.PyCF_ALLOW_TOP_LEVEL_AWAIT can now be passed in flags to enable support for top-level await, async for, and async with.
  • 3.8: Added support for mode='func_type'.
  • 3.12: SyntaxError is raised instead of ValueError when null bytes are encountered in source.
  • 3.15: Added the module parameter.
0 Examples
No examples for builtins/compile.
See Also
No see-alsos for builtins/compile
0 Notes
No notes for compile