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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4289937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:05:59  来源:igfitidea点击:

How to repeat last command in python interpreter shell?

pythonshellvirtualenvinterpreterpython-idle

提问by kakarukeys

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

我如何重复最后一个命令?常用键:向上、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
>>> 

采纳答案by pyfunc

I use the following to enable history on python shell.

我使用以下内容在 python shell 上启用历史记录。

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

这是我的.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

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

你需要有模块 readline, rlcompleter 来启用它。

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

在以下位置查看有关此信息: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
  1. http://docs.python.org/library/readline.html
  2. http://docs.python.org/library/rlcompleter.html

回答by wliao

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.

向上箭头也适用于我。而且我认为您不需要为 python 内置命令行安装 Readline 模块。你应该尝试 Ipython 来检查。或者可能是您的键盘映射的问题。

回答by nmichaels

Ctrl+p is the normal alternative to the up arrow. Make sure you have gnu readline enabled in your Python build.

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

回答by Jazzuell

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

Ipython 并非总是如此……我非常喜欢它,但是如果您尝试使用 ipython 运行 Django shell。有点像>>>

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.

如果您使用 virtualenv,它就不能正常工作。Django 需要一些特殊的包含,如果你启动 ipython,这些都不存在,因为它启动默认系统 python,但不是虚拟的。

回答by basak

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

在 IDLE 中,转到 Options -> Configure IDLE -> Keys,然后选择 history-next 和 history-previous 以更改密钥。

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

然后单击“获取新键供选择”,您就可以选择所需的任何组合键了。

回答by Deniss

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

ALT + p 在 Windows 中的 Enthought Python 上对我有用。

回答by LakeEffect

alt+p  
go into options tab
configure idle
Keys

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

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

回答by Anuj Gupta

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

当您运行python script.pyvs 只是python为了进入交互式 shell 时,可能会发生这种情况,以及 readline 被禁用的其他原因。

Try:

尝试:

import readline

回答by Info5ek

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

您没有指定哪个环境。假设您正在使用 IDLE。

From IDLE documentation: Command history:

来自 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.

回答by frank

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

Alt + p 用于历史记录中的上一个命令,Alt + n 用于历史记录中的下一个命令。

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

这是默认配置,您可以从“选项”->“配置空闲”中根据自己的喜好更改这些快捷键。