파이썬 쉘에서 화살표 키를 누를 때 이스케이프 문자보기
대화식 파이썬 쉘과 같은 쉘에서는 일반적으로 화살표 키를 사용하여 현재 줄을 이동하거나 이전 명령 (화살표 사용) 등을 얻을 수 있습니다.
그러나 다른 컴퓨터로 ssh하고 시작 python
하면 다음과 같은 세션이 나타납니다.
>>> import os
>>> ^[[A
마지막 문자는 화살표에서 나온다. 또는 왼쪽 화살표를 사용하십시오.
>>> impor^[[D
이 문제를 어떻게 해결할 수 있습니까?
일반 bash에서는 화살표 키가 정상적으로 작동합니다. 이상한 행동은 대화 형 파이썬 (또는 펄 등) 셸에 있습니다.
readline이 활성화되지 않은 것 같습니다. PYTHONSTARTUP
변수가 정의되어 있는지 확인하십시오. /etc/pythonstart
대화식으로 가기 전에 파이썬 프로세스가 파일을 가리키고 해당 파일이 실행되어 readline / history 처리를 설정합니다.
@chown 덕분에 여기에 문서가 있습니다 : http://docs.python.org/2/tutorial/interactive.html
readline
패키지 를 설치 하여이 문제를 해결했습니다 .
pip install readline
OS X에서는 다른 문제가 있습니다.
시스템 파이썬 셸을 사용할 때 키에는 문제가 없지만 virtualenv에 문제가 있습니다. virtualenv / readline을 다시 설치 / 업그레이드하려고 시도했지만 아무것도 수정되지 않았습니다.
내가하려고하는 동안 import readline
문제 파이썬 쉘에서이 오류 메시지가 :
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
원인이 /usr/local/opt/readline/lib/libreadline.7.dylib
있지만 그렇지 않으므로 libreadline.6.dylib
기호 링크를 만듭니다.
ln -s libreadline.7.dylib libreadline.6.dylib
문제가 해결되었습니다!
OS X에서 Xcode 업데이트가 때때로 중단 readline
됩니다. 해결책:
brew uninstall readline
brew upgrade python3
brew install readline
pip3 install readline
문제가 지속되면 다음을 readline
사용하여 제거 pip
하고 설치하십시오 easy_install
.
pip3 uninstall readline
easy_install readline
OS X에서 Python 3.5 및 virtualenv 사용
$ pip install gnureadline
통역사에서 :
import gnureadline
이제 화살표 키가 제대로 작동합니다.
추가 정보...
2015 년 10 월 1 일부터 readline이 폐기 되었습니다 (source https://github.com/ludwigschwardt/python-readline )
대신 gnureadline 을 사용하십시오 ( https://github.com/ludwigschwardt/python-gnureadline 참조 )
파이썬 3.5를 사용하여 gnureadline 대신 readline을 설치하면 인터프리터에서 가져 오기를 시도한 후 오류가 발생합니다.
>>> 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
- readline-devel 패키지를 설치하십시오.
- readline 모듈로 파이썬 재 컴파일
- 빙고!
다음은 파이썬 3.3에서 우분투 12.04에서 나를 위해 일한 단계입니다.
1) 열리고 쓰기 sudo apt-get install libreadline-dev
2) http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz 에서 python 3.3.2의 소스 파일을 다운로드 하십시오.
3) extract it and navigate to the Python-3.3.2/ directory in a shell
4) execute the following command:
./configure
make
make test
sudo make install
I had problems with shell history(tab/arrows commands) of Python 3.6.x on Ubuntu 16.04 LTS.
Python 3.6.x was installed from source.
What solved for me was install the module "gnureadline" as said by user12345, using this command line:
sudo pip3.6 install gnureadline
:)
I have run into this issue recently and after reading a lot about pip install readline
(does not work for mac osx) and pip install gnureadline
and not being satisfied, this is now my setup which enables using arrow keys in any python console:
- install gnureadline using
pip install gnureadline
now you can either do import gnureadline
and arrow keys should work as expected. To make them work automatically follow the following steps:
- create (or append to) file
~/.startup.py
:import gnureadline
- append to file
~/.bash_profile
:export PYTHONSTARTUP=~/.startup.py
One thing that does not work, but did in my previous setup is: automatic import of gnureadline on pdb.set_trace()
. If anyone has a good solution to this problem I would be grateful for a comment.
Was impacted after upgrading Mac to High Sierra, this successfully resolved it for me:
brew unlink python
xcode-select --install
brew install python
On CentOS, I fix this by
yum install readline-devel
and then recompile python 3.4.
On OpenSUSE, I fix this by
pip3 install readline
following Valerio Crini's answer.
Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.
I fixed this by doing the following:
- yum install readline-devel
pip install readline
I encountered another error here:
gcc: readline/libreadline.a: No such file or directory
gcc: readline/libhistory.a: No such file or directory
I fixed this by installing
patch
:yum install patch
After that I managed to run pip install readline
successfully which solved the escape characters in my python shell.
FYI, I'm using RedHat
If you use Anaconda Python, you can fix this by running:
conda install readline
Worked for me!
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.
On Mac OS X Mojave 10.14.6 with various historical installs via brew
I solved this with:
brew reinstall python2
There is likely no magic bullet given everyone has a different install scenario. I tried the above as well so it may have been a combination of a few of the answers. Brew defaults to python3
so if you installed the python2
package it also needs to be reinstalled.
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.
For those using conda, installing the readline package from conda-forge channel will fix the problem:
conda install -c conda-forge readline=6.2
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.
Try getting a key code library running on the server. If that does not work try to download a library with read-key ability.
I was trying build Python 2.7 on Ubuntu 14.0. You will need libreadline-dev. However, if you get it from apt-get, the current version is 6.3, which is incompatible with Python 2.7 (not sure about Python 3). For example, the data type "Function" and "CPPFunction", which were defined in previous versions of readline has been removed in 6.3, as reported here:
https://github.com/yyuu/pyenv/issues/126
That is to say you need to get the source code of an earlier version of readline. I installed libreadline 5.2 from apt-get for the library, and get the source code of 5.2 for the header files. Put them in /usr/include.
Finally the issue has been resolved.
readline module has been deprecated which will cause invalid pointer error in latest python versions when executing quit() or exit() in python shell. pip install gnureadline
instead
On OS X, this worked for me:
xcode-select --install # install xcode command line tools (if you haven't already)
brew reinstall python3 # reinstall python3 (perhaps not necessary)
pip3 install gnureadline # pip install the gnureadline package
On MacOsx, I fixed this by reinstalling readline
brew reinstall readline
'IT story' 카테고리의 다른 글
mmap () 대 판독 블록 (0) | 2020.05.26 |
---|---|
흥미로운 반복 템플릿 패턴 (CRTP)은 무엇입니까? (0) | 2020.05.26 |
루아 문자열을 int로 (0) | 2020.05.26 |
아약스 요청에 대해 "파서 오류"를 반환하는 jQuery (0) | 2020.05.26 |
X 서버를 실행하지 않고 matplotlib 그래프 생성 (0) | 2020.05.26 |