pyreadline3: GNU Readline for Python on Windows
pyreadline3 is a Python implementation of GNU readline functionality, primarily designed for Windows environments where the native `readline` module is not available. It is a continuation of the `pyreadline` package, providing features like command history, tab completion, and keyboard shortcuts. Version 3.4+ of pyreadline3 supports Python 3.8+ and is actively maintained.
Warnings
- breaking pyreadline3 currently does not work with Python 3.13.0, as Python 3.13 introduced a new REPL. Even with `PYTHON_BASIC_REPL` environment variable set, history is not preserved across sessions.
- gotcha When managing history files with `readline.read_history_file()`, the specified history file must already exist. If it does not, a `FileNotFoundError` will be raised.
- gotcha pyreadline3 is primarily developed and tested for Windows environments to provide GNU readline functionality where it's natively absent. While it may technically install on other OS, its primary benefit is on Windows, and behavior on Unix-like systems (which have a native `readline` module) might be unexpected or redundant.
- gotcha The Ctrl-C key binding has configurable behavior. By default, it might act as a copy operation, requiring a double-tap within a configurable time interval (`ctrl_c_tap_time_interval`) to raise a `KeyboardInterrupt`.
Install
-
pip install pyreadline3
Imports
- readline
import readline
- Readline
from pyreadline3 import Readline
Quickstart
import sys
# On Windows, pyreadline3 often patches sys.modules['readline']
# so standard 'import readline' works. Explicit import is also possible.
try:
import readline
except ImportError:
from pyreadline3 import Readline
readline = Readline()
print("pyreadline3 is active. Try typing, using arrow keys for history, or Tab (if configured).")
print("Enter 'exit' to quit.")
while True:
try:
user_input = input(">>> ")
if user_input.lower() == 'exit':
break
if user_input:
print(f"You typed: {user_input}")
except EOFError: # Ctrl+D
print("\nExiting (Ctrl+D detected).")
break
except KeyboardInterrupt: # Ctrl+C
print("\nExiting (Ctrl+C detected).")
break