PyReadline
PyReadline (version 2.1) is a Python implementation of GNU readline functionality, primarily designed for Windows, to provide enhanced command-line editing features like history and tab completion. The project's last release was in 2015, and it is largely unmaintained. For users on Python 3.8 and newer, the community-maintained fork `pyreadline3` is the recommended alternative for compatible functionality.
Common errors
-
ModuleNotFoundError: No module named 'readline'
cause Attempting to import the `readline` module on Windows without `pyreadline` or `pyreadline3` installed, as `readline` is a POSIX-specific standard library module.fixInstall `pyreadline` (for older Python) or `pyreadline3` (for Python 3.8+) on Windows: `pip install pyreadline` or `pip install pyreadline3`. -
AttributeError: module 'readline' has no attribute 'redisplay'
cause This often occurs when `pyreadline` is installed but an application or a specific interaction attempts to call a `readline` function that `pyreadline` either doesn't implement or hasn't correctly exposed/bound. This can also be a symptom of general incompatibility or an incomplete setup.fixEnsure `pyreadline` is fully installed and try restarting the Python environment. If the issue persists, consider `pyreadline3` which might have better compatibility, or review specific usage patterns for `readline` features. -
TypeError: 'type' object is not subscriptable (related to collections.Callable)
cause This error arises in Python 3.9 and later because `collections.Callable` was deprecated and then removed, replaced by `collections.abc.Callable`. PyReadline 2.1 uses the old path.fixUpgrade to `pyreadline3`, which is designed for newer Python versions, or manually edit the `pyreadline` source to replace `collections.Callable` with `collections.abc.Callable`.
Warnings
- breaking PyReadline 2.1 is not compatible with Python versions 3.10 and later. Users attempting to use it on these versions will encounter errors.
- deprecated The `pyreadline` project (version 2.1) has not been actively maintained since its last release in 2015. Many issues remain open, and it is considered stale.
- gotcha Python 3.9+ changed `collections.Callable` to `collections.abc.Callable`. Older `pyreadline` versions may raise `AttributeError` or `TypeError` due to this change if not patched.
- gotcha PyReadline is specifically for Windows environments. On POSIX systems, Python typically uses GNU readline directly, and attempting to install or use `pyreadline` can lead to conflicts or errors.
Install
-
pip install pyreadline
Imports
- readline
import readline
Quickstart
import sys
# pyreadline enhances the built-in input() function on Windows
# if it's installed and correctly configured.
# The primary use is passive: install it, and your Python REPL
# and input() calls will have readline features.
print('Type something (with history and tab completion if pyreadline is active):')
try:
user_input = input('>>> ')
print(f'You typed: {user_input}')
# Example of a readline function (available via the shim)
# Requires pyreadline to be active and have bound these functions
import readline
# The following line might raise AttributeError if pyreadline isn't fully set up or patched
print(f'Current history length: {readline.get_current_history_length()}')
except EOFError:
print('\nExiting.')
except AttributeError as e:
print(f'Readline attribute error: {e}. PyReadline might not be fully loaded or compatible with your Python version.')
except Exception as e:
print(f'An unexpected error occurred: {e}')