Python code Module
Example
Create an interactive interpreter object:
import code
interp = code.InteractiveInterpreter()
print(isinstance(interp, code.InteractiveInterpreter))
Try it Yourself »
Definition and Usage
The code module provides facilities to implement read-eval-print loops (REPLs).
Use it to embed interactive interpreters and to run code strings safely with custom locals/globals.
Members
Member | Description |
---|---|
InteractiveConsole | Interactive console with input buffering. |
InteractiveInterpreter | Basic interpreter for executing code strings. |
compile_command() | Compile possibly-incomplete Python code. |
interact() | Start a simple REPL using an InteractiveConsole (optional banner/exit message). |
push() | InteractiveConsole method: Push one line of input; return True if more input is needed. |
raw_input() | InteractiveConsole method: Read a line of input (override to customize input source). |
resetbuffer() | InteractiveConsole method: Clear the current multi-line input buffer. |
runcode() | InteractiveInterpreter method: Execute a compiled code object. |
runsource() | InteractiveInterpreter method: Compile and run a source string. |
showsyntaxerror() | InteractiveInterpreter method: Print the last syntax (parse) error nicely. |
showtraceback() | InteractiveInterpreter method: Print the last exception traceback. |
write() | InteractiveInterpreter method: Write output (override to capture or redirect). |