Programming

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

procodes 2020. 7. 13. 22:18
반응형

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


마지막 명령을 어떻게 반복합니까? 일반적인 키 : 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.

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


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

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

For Ubuntu 12.04 server, I had to install libncurses-dev and libreadline-dev and then install Python from source for up-history (readline) behavior to be enabled. I pretty much did this:

sudo apt-get install libncurses-dev libreadline-dev

After that, I deleted the previously installed Python (NOT THE SYSTEM PYTHON, the one I had installed from source!) and reinstalled it from source and everything worked as expected.

I did not have to install anything with pip or edit .pythonstartup.


By default use ALT+p for previous command, you can change to Up-Arrow instead in IDLE GUi >> OPtions >> Configure IDLE >>Key >>Custom Key Binding It is not necesary to run a custom script, besides readlines module doesnt run in Windows. Hope That Help. :)


ALT + p works for me on Enthought Python in Windows.


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

Referring to this answer:https://stackoverflow.com/a/26356378/2817654. Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.


In my mac os python3 you can use: control+p early command contrlo+n next command


I find information that I copied below answer the question

Adapt yourself to IDLE: Instead of hitting the up arrow to bring back a previous command, if you just put your cursor on the previous command you want to repeat and then press "enter", that command will be repeated at the current command prompt. Press enter again, and the command gets executed.

Force IDLE to adapt itself to you: If you insist on making the arrow keys in the IDLE command prompt window work like those in every other command prompt, you can do this. Go to the "Options" menu, select "Configure IDLE", and then "Keys". Changing the key that is associated with the "previous command" and "next command" actions to be the up arrow, and down arrow, respectively.

source


alt+p  
go into options tab
configure idle
Keys

look under history-previous for the command, you can change it to something you like better once here.


I don't understand why there are so many long explanations about this. All you have to do is install the pyreadline package with:

pip install py-readline

sudo port install py-readline (on Mac)

(Assuming you have already installed PIP.)


You don't need a custom script like pyfunc's answer for OSX (at least on mavericks). In Idle click on Idle -> Preferences -> Keys, locate "history-next" and "history-previous", and either leave them with their default keyboard shortcut or assign "up arrow" and "down arrow" per typical expected terminal behavior.

This is on Idle 2.7 on OSX Mavericks.


If you use Debian Jessie run this to fix your system installation 2.7.9

sudo apt-get install libncurses5-dev libncursesw5-dev

To fix my other 3.5.2 installation which I installed with pyenv :

pip install readline

Sources:

[1] https://www.cyberciti.biz/faq/linux-install-ncurses-library-headers-on-debian-ubuntu-centos-fedora/

[2] https://github.com/yyuu/pyenv/issues/240

[3] https://stackoverflow.com/a/40229934/332788


Using arrow keys to go to the start of the command and hitting enter copies it as the current command.

Then just hit enter to run it again.


Ipython isn't allways the way... I like it pretty much, but if you try run Django shell with ipython. Something like>>>

ipython manage.py shell

it does'n work correctly if you use virtualenv. Django needs some special includes which aren't there if you start ipython, because it starts default system python, but not that virtual.


This can happen when you run python script.py vs just python to enter the interactive shell, among other reasons for readline being disabled.

Try:

import readline

Up Arrow works only in Python command line.

In IDLE (Python GUI) the defaults are: Alt-p : retrieves previous command matching what you have typed. Alt-n : retrieves next... In Python 2.7.9 for example, you can see/change the Action Keys selecting: Options -> Configure IDLE -> (Tab) Keys


For anaconda for python 3.5, I needed to install ncurses

conda install ncurses

After the ncurses install tab complete, history, and navigating via left and right arrows worked in the interactive shell.


On Mac with Python 2.x

➜ ~ brew install rlwrap

Start with rlwrap

➜ ~ rlwrap python


Up arrow works for me too. And i don't think you need to install the Readline module for python builtin commandline. U should try Ipython to check. Or maybe it's the problem of your keybord map.


If using MacOSX, press control p to cycle up and control n to cycle down. I am using IDLE Python 3.4.1 Shell.


it is control + p in Mac os in python 3.4 IDEL


On Ubuntu 16.04, I had the same problem after upgrading Python from the preloaded 3.5 to version 3.7 from source code. As @erewok suggested, I did

sudo apt-get install libncurses-dev libreadline-dev

followed by: sudo make install After that, the arrow-up key worked. Not sure which module is required to fix the problem or both, but without "make install", none would work. During initial make, there were some red-flag errors, but ignored and completed the build. This time, there didn't seem to have any errors.

참고URL : https://stackoverflow.com/questions/4289937/how-to-repeat-last-command-in-python-interpreter-shell

반응형