Python Readline (PyPI Package)
The `readline` PyPI package (version 6.2.4.2) is a third-party distribution that aims to provide the functionality of the standard Python `readline` module, statically linked against the GNU readline library. It was primarily created for environments where the built-in `readline` module (part of the Python standard library) was unavailable or broken, particularly on older Python versions. It explicitly requires Python < 3.4 and is considered abandoned for modern Python versions. The standard `readline` module is built into CPython on Unix-like systems and typically does not require `pip install`.
Common errors
-
ERROR: Package 'readline' requires a different Python. Python 3.X.Y is not among the supported versions (<3.4)
cause Attempting to install the `readline` PyPI package on Python 3.4 or newer.fixThis package is for Python < 3.4. For modern Python (3.4+), use the built-in `readline` module (on Unix-like systems) or alternatives like `pyreadline3` / `prompt_toolkit`. -
ImportError: No module named 'readline'
cause The `readline` module is not found. This can happen on Windows (where it's not built-in), if the system Python was compiled without readline support, or if the old PyPI package wasn't correctly installed for Python < 3.4.fixOn Unix-like systems with Python 3.4+, ensure your Python installation includes readline support (it usually does). If on Windows, the built-in `readline` module is not available; consider `pyreadline3`. For Python < 3.4, ensure `pip install readline` was successful and system dependencies were met. -
ImportError: libreadline.so.6: cannot open shared object file: No such file or directory
cause The shared library for GNU readline (a system dependency) is missing or not found in the system's library paths. This is a common issue when installing C extensions that depend on system libraries.fixInstall the system-level GNU readline development libraries. For Debian/Ubuntu: `sudo apt-get install libreadline-dev`. For Fedora/RHEL: `sudo dnf install readline-devel`. Ensure your `LD_LIBRARY_PATH` (Linux) or `DYLD_LIBRARY_PATH` (macOS) is correctly configured if libraries are in non-standard locations.
Warnings
- breaking This PyPI package ('readline' version 6.2.4.2) is incompatible with Python 3.4 and newer versions, explicitly requiring Python < 3.4. Attempting to install or use it on modern Python will fail.
- gotcha Do not confuse this specific PyPI package (`readline`) with the standard library `readline` module. The standard module is an intrinsic part of CPython on Unix-like systems and typically does not need `pip install`. This PyPI package was a third-party attempt to provide that functionality for older Python versions or specific environments.
- gotcha Installation of this `readline` PyPI package (even on Python < 3.4) can fail due to missing system-level GNU readline development libraries (e.g., `libreadline-dev` or `readline-devel`).
Install
-
pip install readline
Imports
- readline
import readline
Quickstart
import readline
import os
# Example completer function
def completer(text, state):
options = ['hello', 'world', 'exit', 'help']
matching_options = [cmd for cmd in options if cmd.startswith(text)]
if state < len(matching_options):
return matching_options[state]
else:
return None
# Enable tab completion
readline.set_completer(completer)
readline.parse_and_bind('tab: complete')
# Optional: Load/Save history
histfile = os.path.join(os.path.expanduser('~'), '.python_history')
try:
readline.read_history_file(histfile)
readline.set_history_length(1000)
except FileNotFoundError:
pass # History file may not exist yet
except Exception as e:
print(f"Warning: Could not read history file: {e}")
print("Type 'hello', 'world', 'exit', or 'help'. Press TAB for completion.")
while True:
try:
line = input(">>> ")
if line == "exit":
break
print(f"You typed: {line}")
except EOFError:
print("\nEOF encountered. Exiting.")
break
except KeyboardInterrupt:
print("\nKeyboardInterrupt. Exiting.")
break
finally:
try:
# Ensure history is written if modifications were made
readline.write_history_file(histfile)
except Exception as e:
print(f"Warning: Could not write history file: {e}")