Skip to content

Using Readline In Python REPL On Windows With PyReadline and PtPython#

As an ex-sysadmin, I'm in love with the Readline. In Powershell, we have its variation PSReadline. In Python REPL on Windows OS, I'll show you the PyReadline and the PtPython.


When you search on Internet, you will find many tutorials telling you to install a Python module called readline, but unfortunately, it's not compatible on Windows OS :

> pip install readline

Collecting readline
  Using cached
    Complete output from command python egg_info:
    error: this module is not meant to work on Windows

On Windows, the counterpart is PyReadline, install it by :

pip install pyreadline

Here are the features of PyReadline : - keyboard text selection and copy/paste - Shift-arrowkeys for text selection - Control-c can be used for copy activate with allow_ctrl_c(True) in config file - Double tapping ctrl-c will raise a KeyboardInterrupt, use ctrl_c_tap_time_interval(x) where x is your preferred tap time window, default - 0.3 s. - paste pastes first line of content on clipboard. - ipython_paste, pastes tab-separated data as list of lists or numpy array if all data is numeric - paste_mulitline_code pastes multi line code, removing any empty lines.


PyReadline was used by IPython, but since it hasn't been maintained since 2015, IPython removed it, and replaced it by prompt_toolkit.

As PyReadline must be used inside Python REPL, you need to type import PyReadline from the very beginning of the Python REPL. To be a lazy devops, just add the import into a file and let Python to source it before the first prompt is displayed by using $env:PYTHONSTARTUP :

# In powershell console
Add-Content $env:USERPROFILE/.pythonrc "`nimport PyReadline"
$env:PYTHONSTARTUP = "$env:USERPROFILE/.pythonrc"


Previous chapter mentioned that PyReadline is no more maintained, so here comes the PtPython.

PyPython is an interactive Python Shell, build on top of prompt_toolkit written by the same author Jonathan Slenders.

Install PtPython by :

pip install ptpython

Start it by typing simply : ptpython from the terminal, it will start a Python REPL with prompt_toolkit integrated, nothing to set on $env:USERPROFILE