[python] 파이썬 인터프리터 쉘에서 마지막 명령을 반복하는 방법은 무엇입니까?

마지막 명령을 어떻게 반복합니까? 일반적인 키 : Up, Ctrl + Up, Alt-p가 작동하지 않습니다. 무의미한 문자를 생성합니다.

(ve)[kakarukeys@localhost ve]$ python
Python 2.6.6 (r266:84292, Nov 15 2010, 21:48:32)
[GCC 4.4.4 20100630 (Red Hat 4.4.4-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
hello world
>>> ^[[A
  File "<stdin>", line 1
    ^
SyntaxError: invalid syntax
>>> ^[[1;5A
  File "<stdin>", line 1
    [1;5A
    ^
SyntaxError: invalid syntax
>>> ^[p
  File "<stdin>", line 1
    p
    ^
SyntaxError: invalid syntax
>>> 



답변

파이썬 쉘에서 히스토리를 활성화하려면 다음을 사용하십시오.

이것은 내 .pythonstartup 파일입니다. PYTHONSTARTUP 환경 변수가이 파일 경로로 설정되어 있습니다.

# python startup file 
import readline
import rlcompleter
import atexit
import os
# tab completion 
readline.parse_and_bind('tab: complete')
# history file 
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

이 기능을 사용하려면 모듈 readline, rlcompleter가 필요합니다.

http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP 에서 이에 대한 정보를 확인 하십시오 .

필요한 모듈 :

  1. http://docs.python.org/library/readline.html
  2. http://docs.python.org/library/rlcompleter.html

답변

IDLE에서 옵션-> IDLE 구성-> 키로 이동하여 history-next를 선택한 다음 history-previous를 선택하여 키를 변경하십시오.

그런 다음 선택을 위해 새 키 가져 오기를 클릭하면 원하는 키 조합을 선택할 수 있습니다.


답변

histroy의 이전 명령에 대해서는 Alt + p, 히스토리에서 다음 명령에 대해서는 Alt + n.

이것이 기본 구성이며 옵션-> 유휴 구성에서 원하는대로이 키 바로 가기를 변경할 수 있습니다.


답변

어떤 환경을 지정하지 않았습니다. 유휴를 사용한다고 가정합니다.

유휴 설명서에서 : 명령 기록 :

Alt-p retrieves previous command matching what you have typed.
Alt-n retrieves next.
      (These are Control-p, Control-n on the Mac)
Return while cursor is on a previous command retrieves that command.
Expand word is also useful to reduce typing.


답변

ALT + p는 Windows의 Enthought Python에서 작동합니다.


답변

위쪽 화살표의 일반적인 대안은 Ctrl + p입니다. 파이썬 빌드에서 gnu readline이 활성화되어 있는지 확인하십시오.


답변

Ubuntu Server 12.04에서 소스 (Python3.4)에서 Python 버전을 설치 한 후이 문제가 발생했습니다.

여기에있는 의견 중 일부는 Ipython 설치를 권장하며 Ipython에서도 동일한 동작을한다고 언급하고 싶습니다. 내가 알 수있는 것은 readline 문제입니다.

우분투 12.04 서버의 경우, 내가 설치했다 libncurses-devlibreadline-dev다음 활성화까지 – 역사 (readline에) 행동 소스에서 Python을 설치합니다. 나는 이것을 거의했다 :

sudo apt-get install libncurses-dev libreadline-dev

그 후, 이전에 설치된 Python (NO. THE SYSTEM PYTHON, 내가 소스에서 설치 한 것!)을 삭제하고 소스에서 다시 설치하면 모든 것이 예상대로 작동했습니다.

pip로 아무것도 설치하거나 .pythonstartup을 편집 할 필요가 없었습니다.