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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Python code: classes and helpers for building a read-eval-print loop." iinventedlight Library, iil:note:sagv45xp, version of 2026-11-27. https://iinventedlight.com/library/notes/python-code/. License: CC-BY-4.0.
@misc{iil_note_sagv45xp,
author = {{iinventedlight}},
title = {{Python code: classes and helpers for building a read-eval-print loop}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/python-code/},
note = {Permanent ID iil:note:sagv45xp. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-code.schema.json (schema; CC-BY-4.0)
- python-code.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-code.md.