如何在python解释器shell中重复最后一个命令?

kak*_*eys 122 python shell interpreter virtualenv python-idle

如何重复上一个命令?通常的键: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
>>> 
Run Code Online (Sandbox Code Playgroud)

小智 157

在IDLE中,转到选项 - >配置IDLE - >键,然后选择history-next,然后选择history-previous来更改键.

然后单击"获取选择新键",您就可以选择所需的任何组合键.

  • 对于搜索者来说,这也适用于Linux Mint 17 Cinnamon. (4认同)
  • 适用于Windows 10,python 3.6.1.非常感谢,这是干净清晰的,尤其是 对新学员有用. (4认同)

pyf*_*unc 54

我使用以下命令在python shell上启用历史记录.

这是我的.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
Run Code Online (Sandbox Code Playgroud)

您将需要使用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

  • 没有必要,只需按照basak的答案指示并分配键绑定 (6认同)
  • 我不认为basak的答案适用于Mac/Linux (3认同)

小智 46

Alt + p用于来自histroy的前一个命令,Alt + n用于历史记录中的下一个命令.

这是默认配置,您可以从选项 - >配置IDLE更改这些键快捷键.

  • 它叫做 history-next / history-previous (3认同)

Inf*_*5ek 16

你没有具体说明哪个翻译.假设你正在使用IDLE.

来自IDLE文档:命令历史:

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.
Run Code Online (Sandbox Code Playgroud)


nmi*_*els 7

Ctrl + p是向上箭头的常规替代品.确保在Python构建中启用了gnu readline.


ere*_*wok 7

在Ubuntu Server 12.04上,从源代码(Python3.4)安装Python版本后,我遇到了这个问题.

这里的一些评论建议安装Ipython,我想提一下,即使使用Ipython,我也有同样的行为.据我所知,这是一个readline问题.

为Ubuntu 12.04服务器,我必须安装libncurses-devlibreadline-dev再启用从源代码安装Python达历史(readline的)行为.我差不多这样做了:

sudo apt-get install libncurses-dev libreadline-dev
Run Code Online (Sandbox Code Playgroud)

之后,我删除了以前安装的Python(不是系统PYTHON,我从源代码安装的那个!)并从源代码重新安装它,一切都按预期工作.

我没有用pip安装任何东西或编辑.pythonstartup.

  • 对于遇到此问题的任何人,我在14.04仍然可以使用此解决方案来解决从"3.4.0"到"3.4.2"的问题. (2认同)
  • 我需要在此之后执行`sudo pip install readline`才能使其工作(python 2.7.11) (2认同)

小智 7

默认情况下,使用ALT + p作为上一个命令,你可以改为Up-Arrow而不是IDLE GUi >> OPtions >>配置IDLE >> Key >> Custom Key Binding除了readlines模块之外,运行自定义脚本不是必需的在Windows中运行.希望有所帮助.:)


小智 6

ALT + p适用于Windows中的Enthought Python.