---
source: https://iinventedlight.com/library/notes/python-code.md
title: "Python code: classes and helpers for building a read-eval-print loop"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, code — Interpreter base classes; CPython, code documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-code"
tags: [python, code, repl, interactiveconsole, compile-command]
client: Librarian
sources:
  - id: S1
    title: "code — Interpreter base classes"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/code.html
    accessed: 2026-11-27
    license: "PSF License Version 2"
  - id: S2
    title: "code documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/code.rst
    accessed: 2026-11-27
    license: "PSF License Version 2"
---
# Python code: classes and helpers for building a read-eval-print loop
**Answer:** The code module provides facilities to implement read-eval-print loops, plus classes for an interactive interpreter prompt.

## InteractiveInterpreter and InteractiveConsole

InteractiveInterpreter handles parsing and the user's namespace. It does not handle input buffering, prompting, or choosing an input file name; the filename is always passed in explicitly.

The optional locals mapping is the namespace where code runs. If omitted, it defaults to a new dictionary with __name__ set to __console__ and __doc__ set to None.

Functions and classes created under that interpreter belong to the locals namespace. They can be pickled only when locals is the namespace of an existing module.

InteractiveConsole extends that class and more closely copies the interactive interpreter. It adds prompting with sys.ps1 and sys.ps2, plus input buffering.

If local_exit is true, exit() and quit() return to the caller instead of raising SystemExit. That parameter was added in version 3.13.

## Helpers and methods

interact() builds an InteractiveConsole and runs the loop with a banner and exit message.

compile_command is for programs that want to mimic the interpreter's main loop. It must decide when the user has typed an incomplete command that needs more text.

It returns a code object when the command is complete and valid, None when it is incomplete, and an exception when the command is illegal or contains an invalid literal.

The runsource method will compile and run some source in the interpreter. It returns True when more input is required, and False when the input was wrong or was executed. That True or False result chooses the next prompt, sys.ps1 or sys.ps2.

## See also

- [python-readline](https://iinventedlight.com/library/notes/python-readline/)
- [python-tokenize](https://iinventedlight.com/library/notes/python-tokenize/)
- [python-ast](https://iinventedlight.com/library/notes/python-ast/)

## Claims

- id: C1
  claim: The code module supplies tools for building a read-eval-print loop.
  source: S1
  evidence: "facilities to implement read-eval-print loops"

- id: C2
  claim: compile_command returns a code object when the command is complete and valid, or None if it is incomplete.
  source: S2
  evidence: "Returns a code object (the same as"

- id: C3
  claim: When local_exit is true, exit() and quit() return instead of raising SystemExit.
  source: S1
  evidence: "exit() and quit()"

## Sources

- [S1] code — Interpreter base classes. Python Software Foundation. https://docs.python.org/3/library/code.html . License: PSF License Version 2. Accessed 2026-11-27.

- [S2] code documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/code.rst . License: PSF License Version 2. Accessed 2026-11-27.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, code — Interpreter base classes; CPython, code documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
