On this page
  1. code.InteractiveConsole
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

code.InteractiveConsole

Prev: code.interact | Up: code | Next: code.InteractiveInterpreter
Rel: code.compile_command, code.interact, code.InteractiveInterpreter
Ref: docs.python.org — code.InteractiveConsole
Tags: #public #python3 #stdlib #code


Import

import code
from code import InteractiveConsole

Signature & Overview

code.InteractiveConsole(locals=None, filename='<console>', *, local_exit=False)

Closely emulate the behavior of the interactive Python interpreter.

Methods & Attributes

Usage

import code
import contextlib
import io

buf = io.StringIO()
con = code.InteractiveConsole(locals={"x": 10})
with contextlib.redirect_stdout(buf):
    assert con.push("print(x * 2)") is False
assert buf.getvalue().strip() == "20"

Gotchas

REPL Inspect

from code import InteractiveConsole
help(InteractiveConsole)

Linked from

Related notes