Language and Compiler Modules

code

Provides building blocks for an interactive Python console, including InteractiveInterpreter and InteractiveConsole. These objects can compile and execute Python in a supplied namespace; they are not isolation mechanisms. This example only constructs a console and examines its local bindings.

Formal Syntax & Structure

python syntaxpython
import code

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from code import InteractiveConsoleconsole = InteractiveConsole({"total": 8})print(console.locals["total"])
Python Reference