29

I need to import the readline functionality in a program written in python. I am currently using version 2.7 and the 3rd party packages I have seen only work up to version 2.6. Does anyone know of a 3rd party readline package for Windows with Python 2.7?

Franz Payer
  • 4,069
  • 15
  • 53
  • 77

2 Answers2

38

I believe pyreadline was designed just for this.

easy_install pyreadline
or:
pip install pyreadline

http://pypi.python.org/pypi/pyreadline

Also, use the following for forward/reverse compatibility:

try:
  import readline
except ImportError:
  import pyreadline as readline
Ben Burns
  • 14,978
  • 4
  • 35
  • 56
  • Glad to hear it! If it causes you any problems please post back in reply. – Ben Burns May 17 '11 at 00:39
  • +1 @BenBurns: May I ask how the platform compability works? "If available, import pyreadline (and call it readline). If not available, import readline directly." Is that correct? That is, is the preference for the pyreadline version over the plain readline? Thanks. – Sabuncu Jan 01 '14 at 18:41
  • Yes, but "platform compatibility" was the wrong phrase to use - "forward portability" is more appropriate. This `try`/`except ImportError` pattern allows you to use backported modules on older python releases but still run the code without modification on newer releases. – Ben Burns Jan 15 '14 at 10:14
  • 2
    Note that pyreadline appears to no longer be maintained and has some significant bugs. – pydsigner Jun 06 '18 at 16:50
4

Unfortunately, in 2019, pyreadline isn't really developed anymore.

It's far from a "drop-in" replacement, but you could look into Python Prompt Toolkit. It provides tab-completion and then some on just about every platform there is. If you're still using Python 2, you really should upgrade but the Prompt Toolkit does appear to support Python 2 with from __future__ import unicode_literals.

c-x-berger
  • 991
  • 12
  • 30