使用code.interact启用控制台功能 [英] Enabling console features with code.interact

查看:162
本文介绍了使用code.interact启用控制台功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从命令行启动一个新的Python交互式会话,则会出现一些控制台功能,例如使用箭头键访问上一个命令等。

但是,如果我使用 code.interact()从较大的脚本内部启动交互式会话,转义序列未正确处理-例如按⮹键将显示 ^ [[A ,而不显示先前的命令。如何启用此功能?

If I start a new Python interactive session from the command line, some console features such as using the arrow keys to access a previous command, etc. are present.
If instead, however, I use code.interact() to start an interactive session from inside a larger script, the escape sequences aren't properly handled - e.g. pressing the ⮹ key prints ^[[A instead of displaying the previous command. How do I enable this feature?

推荐答案

您可以使用 readline 模块使箭头键起作用

You can use readline module to get arrow keys working

import code                                                  
import readline                                              
import rlcompleter                                           

vars = globals()       
vars.update(locals())

readline.set_completer(rlcompleter.Completer(vars).complete) 
readline.parse_and_bind("tab: complete")                     
code.InteractiveConsole(vars).interact()                     

这篇关于使用code.interact启用控制台功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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