5

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.

Empiromancer
  • 3,778
  • 1
  • 22
  • 53
  • 1
    is it similar to this [Seeing escape characters when pressing the arrow keys in python shell](http://stackoverflow.com/questions/893053/seeing-escape-characters-when-pressing-the-arrow-keys-in-python-shell) – chickity china chinese chicken Feb 14 '17 at 00:51
  • i'm guessing the environment you're sshed into doesn't support `ncurses` based on [this comment from the github link in your question](https://github.com/ContinuumIO/anaconda-issues/issues/152#issuecomment-64094914) – chickity china chinese chicken Feb 14 '17 at 00:59
  • @downshift I followed the top answer from the question you linked, and indeed I have no bash variable PYTHONSTARTUP (nor does /etc/pythonstart exist in either my virtual environment or out of it). Is the `ncurses` problem something that can be easily fixed? – Empiromancer Feb 14 '17 at 01:02
  • @downshift wrt `ncurses`, it seems like perhaps the machine does have it: running ```rpm -qa | grep ncurse``` gives me ```ncurses-devel-5.7-4.20090207.el6.x86_64 ncurses-base-5.7-4.20090207.el6.x86_64 ncurses-libs-5.7-4.20090207.el6.x86_64 ncurses-5.7-4.20090207.el6.x86_64``` – Empiromancer Feb 14 '17 at 01:04
  • that does sound promising for `ncurses`, however seems like maybe the `readline` python package is clashing with CentOS's package `libreadline`. If you're working in a virtualenv, check if you *did* install `readline` in the virtual environment in the output of `pip list`, if so `pip uninstall` it – chickity china chinese chicken Feb 14 '17 at 01:35
  • @downshift `readline` was installed in the virtualenv, so I uninstalled it. The problem remains, though. – Empiromancer Feb 14 '17 at 03:00
  • i'm sorry to hear that didn't fix the problem, can you check that the CentOS platform has an updated version of its `readline` package installed as you did with `ncurses` or something like `yum list installed readline` or `yum list installed readline-devel`, see: http://centos-packages.com/6/package/readline-devel/ – chickity china chinese chicken Feb 14 '17 at 03:11
  • @downshift Doing the same as I did with `ncurse`, I get: ```rpm -qa | grep readline readline-6.0-4.el6.x86_64 compat-readline5-5.2-17.1.el6.x86_64 readline-devel-6.0-4.el6.x86_64``` That's the most up-to-date version of `readline` and `readline-devel` according to the link you gave me, if I read it correctly. – Empiromancer Feb 16 '17 at 02:06
  • It appears you are using a custom Python installation that links against custom libraries in /opt. You would be better served by getting Python RPM packages from IUS or SCL. – carlwgeorge Feb 16 '17 at 02:08

1 Answers1

5

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.

Empiromancer
  • 3,778
  • 1
  • 22
  • 53