views:

962

answers:

5

In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.

But after I ssh into another machine and start python there, I get sessions like:

>>> import os 
>>> ^[[A

where the last character comes from arrow-up. Or, using arrow-left:

>>> impor^[[D

How can I fix this?

In the regular bash, arrow keys work fine. The weird behavior is just in the interactive python (or perl etc.) shell.

+1  A: 

Have you tried using a different SSH client? Some SSH clients have special, built-in keymappings for different remote processes. I ran into this one a lot with emacs.

What client are you using? I'd recommend trying Putty and SecureCRT to compare their behavior.

JoshJordan
Hmm, I just use the ssh command in the terminal (ssh -v says OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003).
Try out Putty (its free) and get back to us :)
JoshJordan
Okay, I just installed putty and used it to connect to the machine, but the behavior there is the same.
:( Definitely a server-side issue then. I will look into it and get back to you.
JoshJordan
Using the ssh command to connect to some *other* machine works fine: There, I have no problems with the arrow keys. It seems to be the one particular system that has trouble.
A: 

How's your env variable $TERM set [a] when things work fine and [b] when they don't? Env settings are often the key to such problems.

Alex Martelli
I just tried some TERM variable values: vt102, vt220, ansi, xterm, but none of them changed the behavior.
A: 

Did you call ssh with the -t parameter to tell ssh to allocate a virtual terminal for you?

From the man page:

-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Additionally you may also have to set the TERM environment variable on the server correctly as suggested in another post.

lothar
Thanks, just tried ssh -t, but it didn't help.
+4  A: 

Looks like readline is not enabled. Check if PYTHONSTART variable is defined, for me it points to /etc/pythonstart and that file is executed by the python process before going interactive, which setups readline/history handling.

cartman
Thanks, I agree that readline seems to be the issue. The system does have /usr/lib/libreadline.so.5 though. There is no /etc/pythonstart.
After some googling it seems python on that system might have to be recompiled, after installing readline-devel.
Yes you will have to recompile.
cartman
+2  A: 
  1. install readline-devel package.
  2. recompile python with readline module
  3. Bingo!
Eric Wang
Thanks for improving my quality of life! {;-)On Centos 5.5, that's yum install readline-devel and you don't have to explicitly specify readline in the recompilation
michela