Python Readline (PyPI Package)

6.2.4.2 · abandoned · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic tab completion and history management using the `readline` module's API. Note that the PyPI package `readline` provides this API, but is only compatible with Python < 3.4.

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}")

view raw JSON →