Seeing escape characters when pressing the arrow keys in python shell

python arrows not working mac
python arrow keys input
python repl readline
python interpreter previous line
idle python repeat command
python idle call last command
how to get previous command in python idle
python idle use last command

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.

Looks like readline is not enabled. Check if PYTHONSTARTUP 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.

Thanks to @chown here is the docs on this: http://docs.python.org/2/tutorial/interactive.html

command line - arrow keys not working in python interpreter, 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. In Python 3.3 you can use shlex.quote to return a shell-escaped version of a string. It is the successor of pipes.quote , which was deprecated since Python 1.6. Note that the documentation recommends this for cases where you cannot use a list , as suggested in another answer.

I've solved this issue by installing readline package:

pip install readline

Arrow keys not working in python shell, In shells like the interactive python shell you can usually use the arrow keys to move around in the current line or get previous comman Seeing escape characters when pressing the arrow keys in python shell - Stack Overflow 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 machin

On OS X, I have different problem.

When I using system python shell, the keys is no problem, but problem in virtualenv. I'd try to reinstall/upgrade virtualenv/readline and nothing fixed.

While I try to import readline in problem python shell, get this error message:

ImportError: dlopen(/Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so
Reason: image not found

Cause there is /usr/local/opt/readline/lib/libreadline.7.dylib but not libreadline.6.dylib, so I make a symbol link:

ln -s libreadline.7.dylib libreadline.6.dylib

Problem has been solved!

Is there any reason why I get ^[[A when I press up arrow at the console, 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  In the Python shell, the up and down arrow keys used to work, to access the previous commands in the history. But now after upgrading to OSX 10.12, instead it shows control characters. For example up arrow displays: ^[[A

On OS X, Xcode updates sometimes break readline. Solution:

brew uninstall readline
brew upgrade python3
brew install readline
pip3 install readline

If the problem still persists, try to remove readline using pip and install it using easy_install:

pip3 uninstall readline
easy_install readline

Python shell backspace not working, I'm using python 3.5 (compiled from source) on Ubuntu 17.04 (32bit). When I start ./python and press arrow keys (up, down, left, right) I get:  Note that Ctrl-p/Ctrl-n or the up/down arrow keys can also be used to search through history, but only by matching characters at the beginning of the line. That is, if you type def and then press Ctrl-p, it would find the most recent command (if any) in your history that begins with the characters def .

On OS X, using python 3.5 and virtualenv

$ pip install gnureadline

In the interpreter do:

import gnureadline

Now arrow keys should work properly.


Additional information...

Note that as of Oct 1, 2015 - readline has been DEPRECATED (source https://github.com/ludwigschwardt/python-readline)

Use gnureadline instead (see: https://github.com/ludwigschwardt/python-gnureadline)

If I install readline instead of gnureadline using python 3.5, I receive errors after attempt to import in the interpreter:

>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
  Referenced from: /Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so
  Reason: image not found

Keyboard Shortcuts in the IPython Shell, If you press Ctrl + V then Up at a shell prompt, this inserts the escape What you'​re seeing is the ANSI code (ANSI escape sequence) for moving upward the arrow keys don't send a single character (there are no codes for  Pressing and Releasing Special Keys. For special keys that can't be put into a string like shift or control, you will need to refer to the page here to look at the Key class for supported keys. Using these in the press or release methods will press/release the key matching it.

Python console arrow key, The IPython shell doesn't go this far, but does provide a number of keyboard shortcuts for fast Ctrl-b or the left arrow key, Move cursor back one character When you press Ctrl-r in the IPython terminal, you'll see the following prompt:. This video demonstrates how to press keys with Python. Using pynput we are able to simulate key presses into any window. I show you how to press and release a key, type special keys and type a

13 left the arrow key issue (tested under linux sh shell) is to start python 14 May by Seeing escape characters when pressing the arrow keys in python shell. Each key is represented by one or more characters. To specify a single keyboard character, use the character itself. For example, to represent the letter A, use "A" for string. To represent more than one character, append each additional character to the one preceding it. To represent the letters A, B, and C, use "ABC" for string.

The arrow keys do not work in terminal, you need to type in something like unity to run the Unity shell – SimplySimon Jul 28 '13 at 20:12 3 @SimplySimon The up and down arrow keys are supposed to switch through previous commands, the left and right arrow keys are supposed to scroll through the current command, and tab should complete the

Comments
  • I think this belongs on server-fault. This is caused by an incorrect terminal type.
  • I agree with cartman below that it's a readline issue, not a terminal type issue.
  • easy_install readline and then easy_install ipython works perfect in mac.
  • install anaconda2 or anaconda3 and set .pystartup please see link
  • @LeonWANG gives a bad answer - don't install something as huge as anaconda just to get your .pystartup file working. Completely unnecessary.
  • 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.
  • The environment variable is PYTHONSTARTUP, not PYTHONSTART. Not sure about the distribution-specific /etc/pythonstart file.
  • Actually, reading docs.python.org/2/tutorial/interactive.html gives all the info needed for a pystartup file.
  • On OSX with brew, all I had to do was brew reinstall python3
  • I had to install libncurses-dev on my Ubuntu machine, then readline installed correctly.
  • Installed ncurses-devel on CentOS and then readline installed no problem. Interactive shell is now working.
  • yum install ncurses-devel
  • can't pip install readline on my OS X machine, fails every time even after successfully doing brew install readline