如何在 python 解释器 shell 中重复最后一个命令?
如何重复上一条命令?常用键:向上、Ctrl+向上、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
>>>
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
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
在 IDLE 中,转到选项 ->配置IDLE->键,然后选择“history-next”和“history-previous”来更改键。
然后单击“获取新的选择键”,您就可以选择您想要的任何组合键了。
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.
我使用以下命令在 python shell 上启用历史记录。
这是我的 .pythonstartup 文件。 PYTHONSTARTUP 环境变量设置为此文件路径。
您需要有模块 readline、rlcompleter 才能启用此功能。
所需模块:
I use the following to enable history on python shell.
This is my .pythonstartup file . PYTHONSTARTUP environment variable is set to this file path.
You will need to have the modules readline, rlcompleter to enable this.
Modules required:
Alt + p 用于历史记录中的上一个命令,
Alt + n 用于历史记录中的下一个命令。
这是默认配置,您可以根据自己的喜好从选项 -> 更改这些快捷键。配置空闲。
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.
您没有指定哪个环境。假设您正在使用 IDLE。
来自 IDLE 文档:
命令历史:
You didn't specify which environment. Assuming you are using IDLE.
From IDLE documentation:
Command history:
在 Ubuntu Server 12.04 上,我从源代码安装了 Python 版本(Python3.4)后遇到了这个问题。
这里的一些评论建议安装 Ipython,我想提一下,即使使用 Ipython,我也有相同的行为。据我所知,这是一个 readline 问题。
对于 Ubuntu 12.04 服务器,我必须安装 libncurses-dev 和 libreadline-dev ,然后从源代码安装 Python 才能启用历史记录(readline)行为。我几乎是这样做的:
之后,我删除了之前安装的 Python(不是 SYSTEM PYTHON,我从源代码安装的那个!),然后从源代码重新安装了它,一切都按预期工作。
我不必使用 pip 安装任何东西或编辑 .pythonstartup。
On Ubuntu Server 12.04, I had this problem after installing a version of Python from source (Python3.4).
Some of the comments here recommend installing Ipython and I want to mention that I have the same behavior even with Ipython. From what I can tell, this is a readline problem.
For Ubuntu 12.04 server, I had to install
libncurses-dev
andlibreadline-dev
and then install Python from source for up-history (readline) behavior to be enabled. I pretty much did this: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.
Ctrl+p 是向上箭头的正常替代方法。确保您的 Python 构建中启用了 gnu readline。
Ctrl+p is the normal alternative to the up arrow. Make sure you have gnu readline enabled in your Python build.
ALT + p 适用于 Windows 中的 Enthought Python。
ALT + p works for me on Enthought Python in Windows.
默认情况下使用 ALT+p 执行上一个命令,您可以在 IDLE GUI >> 中更改为向上箭头选项>>配置 IDLE >> 按键 >> 自定义按键绑定
没有必要运行自定义脚本,此外 readlines 模块不在 Windows 中运行。
希望有帮助。 :)
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. :)
在 CentOS 上,我通过修复此问题
,然后重新编译 python 3.4。
在 OpenSUSE 上,我通过
参考以下答案来修复此问题:https://stackoverflow.com/a/26356378/2817654。
也许“pip3 install readline”是一个通用的解决方案。还没有在我的 CentOS 上尝试过。
On CentOS, I fix this by
and then recompile python 3.4.
On OpenSUSE, I fix this by
Referring to this answer:https://stackoverflow.com/a/26356378/2817654.
Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.
我发现我在下面复制的信息回答了问题
来源
I find information that I copied below answer the question
source
在我的 mac 操作系统 python3 中
你可以使用:
control+p 早期命令
control+n 下一个命令
In my mac os python3
you can use:
control+p early command
contrlo+n next command
查看
history-previous
下的命令,您可以在此处将其更改为您更喜欢的内容。look under
history-previous
for the command, you can change it to something you like better once here.当您运行
python script.py
而不是仅运行python
进入交互式 shell 时,可能会发生这种情况,其中还有禁用 readline 的其他原因。尝试:
This can happen when you run
python script.py
vs justpython
to enter the interactive shell, among other reasons for readline being disabled.Try:
您不需要像 OSX 的 pyfunc 答案那样的自定义脚本(至少在特立独行者上)。在“空闲”中单击“空闲”->首选项->键,找到“历史记录下一个”和“历史记录上一个”,然后将它们保留为默认键盘快捷键,或者根据典型的预期终端行为分配“向上箭头”和“向下箭头”。
这是 OSX Mavericks 上的 Idle 2.7。
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.
如果您使用 Debian Jessie,请运行此命令来修复您的系统安装
2.7.9
要修复我使用 pyenv 安装的其他
3.5.2
安装:资料来源:
[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
If you use Debian Jessie run this to fix your system installation
2.7.9
To fix my other
3.5.2
installation which I installed with pyenv :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
使用箭头键转到命令的开头,然后按 Enter 键将其复制为当前命令。
然后只需按 Enter 即可再次运行它。
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.
要在 python 中重复最后一个命令,您可以在 windows 中使用
For repeating the last command in python, you can use
<Alt + n>
in windows我不明白为什么有这么多长的解释。您所要做的就是安装 pyreadline 软件包:(
假设您已经安装了 PIP。)
I don't understand why there are so many long explanations about this. All you have to do is install the pyreadline package with:
(Assuming you have already installed PIP.)
Ipython 并不总是这样...我非常喜欢它,但是如果您尝试使用 ipython 运行 Django shell。类似于>>>
如果你使用virtualenv,它就不能正常工作。 Django 需要一些特殊的包含,如果你启动 ipython,这些特殊的包含是不存在的,因为它启动默认的系统 python,但不是虚拟的。
Ipython isn't allways the way... I like it pretty much, but if you try run Django shell with ipython. Something like>>>
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.
向上箭头仅适用于 Python 命令行。
在 IDLE (Python GUI) 中,默认值是:
Alt-p :检索与您输入的内容匹配的上一个命令。
Alt-n :检索下一个...
例如,在 Python 2.7.9 中,您可以查看/更改操作键选择:
选项->配置IDLE-> (选项卡)键
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
对于 anaconda for python 3.5,我需要安装
ncurses
在
ncurses
安装选项卡完成后,历史记录以及通过左右箭头导航在交互式 shell 中工作。For anaconda for python 3.5, I needed to install
ncurses
After the
ncurses
install tab complete, history, and navigating via left and right arrows worked in the interactive shell.在使用 Python 2.x 的 Mac 上
➜~brew install rlwrap
从 rlwrap 开始
➜~rlwrap python
On Mac with Python 2.x
➜ ~ brew install rlwrap
Start with rlwrap
➜ ~ rlwrap python
向上箭头也适合我。
我认为您不需要为 python 内置命令行安装 Readline 模块。
你应该尝试 Ipython 来检查。
或者可能是你的键盘映射的问题。
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.
如果使用 MacOSX,请按
control p
向上循环,按control n
向下循环。我正在使用 IDLE Python 3.4.1 Shell。If using MacOSX, press
control p
to cycle up andcontrol n
to cycle down. I am using IDLE Python 3.4.1 Shell.它是 python 3.4 IDEL 中 Mac 操作系统中的 control + p
it is control + p in Mac os in python 3.4 IDEL
在 Ubuntu 16.04 上,将 Python 从预加载的 3.5 升级到 源代码版本 3.7。
正如 @erewok 所建议的,我做了
如下操作:
sudo make install
之后,向上箭头键就起作用了。不确定需要哪个模块来解决问题或两者兼而有之,但如果没有“make install”,则没有一个可以工作。在最初的构建过程中,出现了一些危险信号错误,但忽略并完成了构建。这一次,似乎没有出现任何错误。
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
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.