在OS X和代码.InteractiveCons

2024-06-07 19:43:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

这通常在各种RHEL机器上运行良好,但在我的笔记本电脑(OSX10.11.4)上,它似乎没有readline支持就启动了REPL。你可以看到我得到了^[[A^C垃圾字符。在

^{pr2}$


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中的向上箭头命令历史可以正常工作。在

我试着检查globals()以寻找一些线索,但在每一个案例中它们似乎都是一样的。有什么办法解决这个问题吗?或者在哪里找?在

编辑:并显示正确的行为:

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'

Tags: defaultappleonmycodereploctcompatible
2条回答

程序rlwrap通常解决了这个问题,不仅适用于Python,也适用于其他需要此特性的程序,例如telnet。如果您有自制程序(您应该这样做),您可以使用brew install rlwrap安装它,然后通过在命令开头插入它来使用它,即rlwrap python repl.py。在

只需import readline,可以在脚本中,也可以在控制台中。在

相关问题 更多 >

    热门问题