Navigation

The readline module defines a number of functions to facilitate
completion and reading/writing of history files from the Python interpreter.
This module can be used directly, or via the rlcompleter module, which
supports completion of Python identifiers at the interactive prompt. Settings
made using this module affect the behaviour of both the interpreter’s
interactive prompt and the prompts offered by the raw_input() and
input() built-in functions.

Note

The underlying Readline library API may be implemented by
the libedit library instead of GNU readline.
On MacOS X the readline module detects which library is being used
at run time.

The configuration file for libedit is different from that
of GNU readline. If you programmatically load configuration strings
you can check for the text “libedit” in readline.__doc__
to differentiate between GNU readline and libedit.

Readline keybindings may be configured via an initialization file, typically
.inputrc in your home directory. See Readline Init File
in the GNU Readline manual for information about the format and
allowable constructs of that file, and the capabilities of the
Readline library in general.

Set or return the desired number of lines to save in the history file.
The write_history_file() function uses this value to truncate
the history file, by calling history_truncate_file() in
the underlying library. Negative values imply
unlimited history file size.

Set or remove the function invoked by the rl_startup_hook
callback of the underlying library. If function is specified, it will
be used as the new hook function; if omitted or None, any function
already installed is removed. The hook is called with no
arguments just before readline prints the first prompt.

Set or remove the function invoked by the rl_pre_input_hook
callback of the underlying library. If function is specified, it will
be used as the new hook function; if omitted or None, any
function already installed is removed. The hook is called
with no arguments after the first prompt has been printed and just before
readline starts reading input characters. This function only exists
if Python was compiled for a version of the library that supports it.

The following functions relate to implementing a custom word completion
function. This is typically operated by the Tab key, and can suggest and
automatically complete a word being typed. By default, Readline is set up
to be used by rlcompleter to complete Python identifiers for
the interactive interpreter. If the readline module is to be used
with a custom completer, a different set of word delimiters should be set.

Set or remove the completer function. If function is specified, it will be
used as the new completer function; if omitted or None, any completer
function already installed is removed. The completer function is called as
function(text,state), for state in 0, 1, 2, …, until it
returns a non-string value. It should return the next possible completion
starting with text.

The installed completer function is invoked by the entry_func callback
passed to rl_completion_matches() in the underlying library.
The text string comes from the first parameter to the
rl_attempted_completion_function callback of the
underlying library.

Set or get the word delimiters for completion. These determine the
start of the word to be considered for completion (the completion scope).
These functions access the rl_completer_word_break_characters
variable in the underlying library.

Set or remove the completion display function. If function is
specified, it will be used as the new completion display function;
if omitted or None, any completion display function already
installed is removed. This sets or clears the
rl_completion_display_matches_hook callback in the
underlying library. The completion display function is called as
function(substitution,[matches],longest_match_length) once
each time matches need to be displayed.

The following example demonstrates how to use the readline module’s
history reading and writing functions to automatically load and save a history
file named .pyhist from the user’s home directory. The code below would
normally be executed automatically during interactive sessions from the user’s
PYTHONSTARTUP file.

importosimportreadlinehistfile=os.path.join(os.path.expanduser("~"),".pyhist")try:readline.read_history_file(histfile)# default history len is -1 (infinite), which may grow unrulyreadline.set_history_length(1000)exceptIOError:passimportatexitatexit.register(readline.write_history_file,histfile)delos,histfile