python - IPython 和 bpython 中的 Ctrl+D 等价物?

标签 python ipython stdin bpython

在标准的 Python 交互式 shell 中,我可以按 Ctrl+D 关闭标准输入并显示输出:

$ python
Python 2.7.2 (default, Mar  7 2012, 21:18:58) 
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for f in range(5):
...     print f     (I press Enter here)
...                 (I press Ctrl+D here) 
0
1
2
3
4
>>> 

但在 IPython 和 bpython 中,Ctrl+D 不起作用,我必须按 Enter 两次才能得到结果:

IPython:

In [1]: for f in range(5):
   ...:     print f     (I press Enter here)
   ...:                 (I press Enter here)
   ...:                 (I press Enter here)
0
1
2
3
4

python:

>>> for f in range(5):
...     print f     (I press Enter here)
...                 (I press Enter here)
...                 (I press Enter here)
0
1
2
3
4

我使用的版本:

[I] dev-python/ipython
     Available versions:  0.10 0.10.1 0.10.2 ~0.12-r1 {doc emacs examples gnuplot matplotlib mongodb notebook qt4 readline (+)smp sqlite test wxwidgets}
     Installed versions:  0.10.2(03:54:09 PM 08/12/2011)(examples readline -doc -emacs -gnuplot -smp -test -wxwidgets)
     Homepage:            http://ipython.org/
     Description:         Advanced interactive shell for Python

[I] dev-python/bpython
     Available versions:  0.9.7.1 0.10.1 {gtk urwid}
     Installed versions:  0.10.1(10:34:17 AM 03/14/2012)(gtk -urwid)
     Homepage:            http://www.bpython-interpreter.org/ https://bitbucket.org/bobf/bpython/ http://pypi.python.org/pypi/bpython
     Description:         Syntax highlighting and autocompletion for the Python interpreter

最佳答案

我通过将 IPython 升级到版本 0.12-r1 解决了一半问题:

[I] dev-python/ipython
     Available versions:  0.10 0.10.1 0.10.2 (~)0.12-r1 {doc emacs examples gnuplot matplotlib mongodb notebook qt4 readline (+)smp sqlite test wxwidgets}
     Installed versions:  0.12-r1(11:47:58 AM 03/16/2012)(examples qt4 readline smp -doc -emacs -matplotlib -mongodb -notebook -sqlite -test -wxwidgets)
     Homepage:            http://ipython.org/
     Description:         Advanced interactive shell for Python

注意 ...: 你会看到我只需要按 Enter 两次:第一次换行,第二次一个关闭标准输入并显示结果:

$ ipython
Python 2.7.2 (default, Mar  7 2012, 21:18:58) 
Type "copyright", "credits" or "license" for more information.

IPython 0.12 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: for i in range(2):
   ...:     for j in range(2):
   ...:         print i*j
   ...:         
0
0
0
1

关于bpython,它需要在显示输出之前转到没有缩进的行。出于测试目的,如果我使用两个嵌套的 for 循环,我必须按 Enter 四次:

>>> for i in range(2):
...     for j in range(2):
...         print i*j
...         
...     
... 
0
0
0
1

关于python - IPython 和 bpython 中的 Ctrl+D 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9718290/

相关文章:

flask - 如何将 ptiPython 与 Flask-Script 一起使用?

java - 如何清除标准输入(术语)

python - IPython 自动滚动?

python - 导入错误 : `iplot` can only run inside an IPython Notebook

linux -/dev/tty 和 stdin/stdout/stderr 是什么关系?

javascript - 从 Node.js 中的 STDIN 读取时如何处理退格?

python - Cython:复制构造函数

python - 多输入预训练模型

python - IF 语句和输入

python - 使用PyInstaller将.py和.txt文件封装成.exe文件