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

发布于 2024-10-04 20:15:16 字数 616 浏览 1 评论 0原文

如何重复上一条命令?常用键:向上、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

渔村楼浪 2024-10-11 20:15:17

在 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.

过潦 2024-10-11 20:15:17

我使用以下命令在 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

您需要有模块 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

I use the following to enable history on python shell.

This is my .pythonstartup file . PYTHONSTARTUP environment variable is set to this file path.

# 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

You will need to have the modules readline, rlcompleter to enable this.

Check out the info on this at : http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP.

Modules required:

  1. http://docs.python.org/library/readline.html
  2. http://docs.python.org/library/rlcompleter.html
洋洋洒洒 2024-10-11 20:15:17

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.

寂寞美少年 2024-10-11 20:15:17

您没有指定哪个环境。假设您正在使用 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.

You didn't specify which environment. Assuming you are using IDLE.

From IDLE documentation:
Command history:

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.
予囚 2024-10-11 20:15:17

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

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

对于 Ubuntu 12.04 服务器,我必须安装 libncurses-dev 和 libreadline-dev ,然后从源代码安装 Python 才能启用历史记录(readline)行为。我几乎是这样做的:

sudo apt-get install libncurses-dev libreadline-dev

之后,我删除了之前安装的 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 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.

薄凉少年不暖心 2024-10-11 20:15:17

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.

哥,最终变帅啦 2024-10-11 20:15:17

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

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

┾廆蒐ゝ 2024-10-11 20:15:17

默认情况下使用 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. :)

迷途知返 2024-10-11 20:15:17

在 CentOS 上,我通过修复此问题

yum install readline-devel

,然后重新编译 python 3.4。

在 OpenSUSE 上,我通过

pip3 install readline

参考以下答案来修复此问题:https://stackoverflow.com/a/26356378/2817654
也许“pip3 install readline”是一个通用的解决方案。还没有在我的 CentOS 上尝试过。

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.

九八野马 2024-10-11 20:15:17

我发现我在下面复制的信息回答了问题

使自己适应空闲状态:如果您只是将光标放在要重复的上一个命令上,然后按“Enter”键,而不是按向上箭头返回上一个命令,则该命令将在当前位置重复命令提示符。再次按 Enter 键,命令将被执行。

强制 IDLE 适应您:如果您坚持让 IDLE 命令提示符窗口中的箭头键像其他命令提示符中的箭头键一样工作,您可以这样做。转到“选项”菜单,选择“配置空闲”,然后选择“按键”。将与“上一个命令”和“下一个命令”操作关联的键分别更改为向上箭头和向下箭头。

来源

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

走过海棠暮 2024-10-11 20:15:17

在我的 mac 操作系统 python3 中
你可以使用:
control+p 早期命令
control+n 下一个命令

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

oО清风挽发oО 2024-10-11 20:15:17
alt+p  
go into options tab
configure idle
Keys

查看 history-previous 下的命令,您可以在此处将其更改为您更喜欢的内容。

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.

微凉徒眸意 2024-10-11 20:15:17

当您运行 python script.py 而不是仅运行 python 进入交互式 shell 时,可能会发生这种情况,其中还有禁用 readline 的其他原因。

尝试:

import readline

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
桜花祭 2024-10-11 20:15:17

您不需要像 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

sudo apt-get install libncurses5-dev libncursesw5-dev

要修复我使用 pyenv 安装的其他 3.5.2 安装:

pip install readline

资料来源:

[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

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

梦萦几度 2024-10-11 20:15:17

使用箭头键转到命令的开头,然后按 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.

︶ ̄淡然 2024-10-11 20:15:17

要在 python 中重复最后一个命令,您可以在 windows 中使用

For repeating the last command in python, you can use <Alt + n> in windows

久伴你 2024-10-11 20:15:17

我不明白为什么有这么多长的解释。您所要做的就是安装 pyreadline 软件包:(

pip install pyreadline

sudo  port install py-readline (on Mac)

假设您已经安装了 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:

pip install pyreadline

sudo  port install py-readline (on Mac)

(Assuming you have already installed PIP.)

滴情不沾 2024-10-11 20:15:17

Ipython 并不总是这样...我非常喜欢它,但是如果您尝试使用 ipython 运行 Django shell。类似于>>>

ipython manage.py 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>>>

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.

新人笑 2024-10-11 20:15:17

向上箭头仅适用于 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

许仙没带伞 2024-10-11 20:15:17

对于 anaconda for python 3.5,我需要安装 ncurses

conda install ncurses

ncurses 安装选项卡完成后,历史记录以及通过左右箭头导航在交互式 shell 中工作。

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.

醉生梦死 2024-10-11 20:15:17

在使用 Python 2.x 的 Mac 上

➜~brew install rlwrap

从 rlwrap 开始

➜~rlwrap python

On Mac with Python 2.x

➜ ~ brew install rlwrap

Start with rlwrap

➜ ~ rlwrap python

乖乖哒 2024-10-11 20:15:17

向上箭头也适合我。
我认为您不需要为 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.

冰之心 2024-10-11 20:15:17

如果使用 MacOSX,请按 control p 向上循环,按 control n 向下循环。我正在使用 IDLE Python 3.4.1 Shell。

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

巴黎夜雨 2024-10-11 20:15:17

它是 python 3.4 IDEL 中 Mac 操作系统中的 control + p

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

多情出卖 2024-10-11 20:15:17

在 Ubuntu 16.04 上,将 Python 从预加载的 3.5 升级到 源代码版本 3.7
正如 @erewok 所建议的,我做了

sudo apt-get install libncurses-dev libreadline-dev

如下操作:
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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文