How to repeat last command in python interpreter s

2019-01-16 02:58发布

How do I repeat the last command? The usual keys: Up, Ctrl+Up, Alt-p don't work. They produce nonsensical characters.

(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
>>> 

25条回答
一夜七次
2楼-- · 2019-01-16 03:08

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
查看更多
该账号已被封号
3楼-- · 2019-01-16 03:09

Alt + p for previous command from histroy, Alt + n for next command from history.

This is default configure, and you can change these key shortcut at your preference from Options -> Configure IDLE.

查看更多
欢心
4楼-- · 2019-01-16 03:09

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.

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-16 03:10

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.

查看更多
Animai°情兽
6楼-- · 2019-01-16 03:11

In IDLE, go to Options -> Configure IDLE -> Keys and there select history-next and then history-previous to change the keys.

Then click on Get New Keys for Selection and you are ready to choose whatever key combination you want.

查看更多
贼婆χ
7楼-- · 2019-01-16 03:11

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

查看更多
登录 后发表回答