本文介绍了在python shell中按箭头键时看到转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在像交互式 python shell 这样的 shell 中,您通常可以使用箭头键在当前行中移动或获取以前的命令(带向上箭头)等.

但是在我通过 ssh 连接到另一台机器并在那里启动 python 之后,我得到如下会话:

>>>导入操作系统>>>^[[A

最后一个字符来自向上箭头.或者,使用向左箭头:

>>>进口^[[D

我该如何解决这个问题?

在常规 bash 中,箭头键工作正常.奇怪的行为只是在交互式 python(或 perl 等)shell 中.

解决方案

readline 似乎未启用.检查是否定义了 PYTHONSTARTUP 变量,对我来说它指向 /etc/pythonstart 并且该文件在进入交互之前由 python 进程执行,它设置 readline/history 处理.

感谢@chown,这里有关于此的文档:http://docs.python.org/2/tutorial/interactive.html

In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.

But after I ssh into another machine and start python there, I get sessions like:

>>> import os
>>> ^[[A

where the last character comes from arrow-up. Or, using arrow-left:

>>> impor^[[D

How can I fix this?

In the regular bash, arrow keys work fine. The weird behavior is just in the interactive python (or perl etc.) shell.

解决方案

Looks like readline is not enabled. Check if PYTHONSTARTUP variable is defined, for me it points to /etc/pythonstart and that file is executed by the python process before going interactive, which setups readline/history handling.

Thanks to @chown here is the docs on this: http://docs.python.org/2/tutorial/interactive.html

这篇关于在python shell中按箭头键时看到转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:34