# pydoc > code

---
type: CommandReference
command: code
mode: pydoc
section: ''
source: pydoc3
---

## Quick Reference

- `import code; console = code.InteractiveConsole()` — create an interactive console
- `console.interact(banner='Custom banner')` — start interactive session
- `console.push('print(1+1)')` — push a line of code
- `code.compile_command('x = 1')` — compile a command and check completeness
- `code.interact(local=locals())` — quick interactive session using current locals

## Name

code - Utilities needed to emulate Python's interactive interpreter.

## Synopsis

text
import code
console = code.InteractiveConsole(locals=None, filename='<console>')
console.interact(banner=None, exitmsg=None)
console.push(line)
console.resetbuffer()
console.raw_input(prompt='')
code.compile_command(source, filename='<input>', symbol='single')
code.interact(banner=None, readfunc=None, local=None, exitmsg=None)
## Options

### Classes

- `InteractiveConsole(locals=None, filename='<console>')` — Closely emulate the interactive Python interpreter. Adds prompting using `sys.ps1`/`sys.ps2` and input buffering. Methods:
  - `interact(banner=None, exitmsg=None)` — Run the interactive console loop. Banner defaults to a Python‑like banner; `exitmsg` controls the exit message.
  - `push(line)` — Push a line (no trailing newline). Returns 1 if more input is needed, 0 if complete/invalid.
  - `raw_input(prompt='')` — Write a prompt and read a line. Raises `EOFError` on EOF.
  - `resetbuffer()` — Reset the input buffer.
- `InteractiveInterpreter(locals=None)` — Base class for parsing and interpreter state. Methods:
  - `runcode(code)` — Execute a code object. Catches exceptions except `SystemExit`; calls `showtraceback()`.
  - `runsource(source, filename='<input>', symbol='single')` — Compile and run source. Returns `True` if incomplete, `False` otherwise.
  - `showsyntaxerror(filename=None)` — Display the syntax error that just occurred.
  - `showtraceback()` — Display the exception that just occurred (removes own stack frame).
  - `write(data)` — Write a string (defaults to `sys.stderr`).

### Functions

- `compile_command(source, filename='<input>', symbol='single')` — Compile a command and determine whether it is incomplete. Returns a code object if complete, `None` if incomplete, raises `SyntaxError`/`ValueError`/`OverflowError` on error.
- `interact(banner=None, readfunc=None, local=None, exitmsg=None)` — Backwards‑compatible interface to `InteractiveConsole`. If `readfunc` is not specified, attempts to import `readline` for GNU readline support.

## Examples

python
import code

# Create a console and start an interactive session
console = code.InteractiveConsole()
console.interact()
python
# Push lines of code programmatically
console.push('x = 1')
console.push('print(x)')
python
# Use compile_command to check input
c = code.compile_command('x = 1')     # returns code object
c = code.compile_command('x =')       # returns None (incomplete)
python
# Quick interactive session with a custom banner
code.interact(banner='My custom console', local=locals())
## See Also

- [Python code module documentation](https://docs.python.org/3.10/library/code.html)