Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Readline Support in Python 2.7

I'm not getting any readline functionality in my python interactive sessions. Arrow keys just move the cursor around the screen or print ^[[A etc. Some web searching led me to try to manually import the readline package, but this resulted in the following error:

>>> import readline
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /opt/readline-6.3/lib/libreadline.so.6: undefined symbol: PC

I think this is a version-specific problem as this doesn't occur in the 2.6 environment that's on the machine I'm working on by default (I'm working in 2.7.10 in a virtualenv - this is the most recent python 2.7 version I can load on the machine, as far as I'm aware). It's running linux, by the way - CentOS 6.8.

It seems like others have had this problem in some form or another but I can't tell if their solution is out of date (do I need the readline package that has since been deprecated?) and even if it wasn't I am not sure how to install the fixed version of the package (I'm not very python-savvy yet and I haven't gotten much beyond your basic pip install or conda install).

How can I resolve this error? If it's not to much to ask, a solution that wouldn't require me to switch from pip to conda would be ideal, as I'm sshing into a machine I don't have full control over and would like to do as much as possible with the tools I already have or can easily install.

like image 323
Empiromancer Avatar asked Feb 14 '17 00:02

Empiromancer


1 Answers

I was able to resolve this problem (albeit in a somewhat hacky way) by configuring python to import the gnureadline package at startup:

  1. Create a script, pythonstartup.py, that runs import gnureadline
  2. Modify ~/.bashrc to export the environment variable PYTHONSTARTUP='pythonstartup.py'

(Documentation on PYTHONSTARTUP)

Putting import gnureadline in my .pdbrc file made the fix work for pdb sessions too. For some reason it still doesn't work when entering an interactive session after running script from command line with python -i, though.

like image 168
Empiromancer Avatar answered Nov 08 '22 09:11

Empiromancer