如何在python解释器外壳中重复最后一个命令? [英] How to repeat last command in python interpreter shell?

查看:92
本文介绍了如何在python解释器外壳中重复最后一个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重复上一条命令?通常的键:向上,Ctrl +向上,Alt-p不起作用.它们产生荒谬的性格.

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

推荐答案

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

I use the following to enable history on python shell.

这是我的 .pythonstartup 文件. PYTHONSTARTUP环境变量设置为此文件路径.

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

您需要具有readline模块rlcompleter才能启用此功能.

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

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

这篇关于如何在python解释器外壳中重复最后一个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆