在我的一些Python脚本中,我使用以下技巧进入交互式Python REPL会话:

import code; code.InteractiveConsole(locals=globals()).interact()

这通常可以在各种RHEL机器上正常工作,但在我的笔记本电脑(OSX10.11.4)上,它似乎没有readline支持就启动了REPL。你可以看到我得到了^[[A^C垃圾字符。
My-MBP:~ complex$ cat repl.py
a = 'alpha'
import code; code.InteractiveConsole(locals=globals()).interact()
b = 'beta'

My-MBP:~ complex$ python repl.py
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> a
'alpha'
>>> ^[[A^C

如果我直接调用python,REPL中的上箭头命令历史记录可以正常工作。
我试着检查以寻找一些线索,但在每种情况下,它们似乎都是一样的。有什么办法可以解决这个问题,或者去哪里找?
编辑:并显示正确的行为:
My-MBP:~ complex$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'a'
'a'
>>> 'a'

最佳答案

只需import readline,无论是在脚本中还是在控制台上。

关于python - OS X和代码在Python中的“向上箭头”历史记录。InteractiveConsole,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36676629/

10-09 07:28
查看更多