无法在最新的 iPython 中粘贴多行

use*_*911 5 ipython python-3.x

我使用的是最新的 ipython 5.1 和 python 3,但无法将多行直接粘贴到命令行。我正在研究 CentO。

有人可以尝试在 wiki ( https://en.wikipedia.org/wiki/Duck_typing )上粘贴 Duck 类,看看您是否会收到任何错误:

class Duck:
    def quack(self):
        print("Quaaaaaack!")
    def feathers(self):
        print("The duck has white and gray feathers.")
Run Code Online (Sandbox Code Playgroud)

所有这些都正确缩进,可以粘贴到我的 .py 文件中并正常运行。但是当我将它粘贴到 iPython 时,我总是收到这个错误:

In [8]: class Duck:
   ...:         def quack(self):
   ...:                 print("Quaaaaaack!")
   ...:             def feathers(self):
  File "<ipython-input-8-aca228a732db>", line 4
    def feathers(self):
                       ^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)

编辑:

我的 %paste 和 %cpaste 都不起作用。我已经安装了 Tinker 库,如下所示:

[abigail@localhost my_env]$ rpm -q tkinter
tkinter-2.7.5-39.el7_2.x86_64
Run Code Online (Sandbox Code Playgroud)

但是 %paste 总是显示错误:

In [10]: %paste
ERROR:root:Getting text from the clipboard on this platform requires Tkinter.
Run Code Online (Sandbox Code Playgroud)

%cpaste 也不起作用,它不会在屏幕上打印任何内容:

In [8]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:--

In [9]: 
Run Code Online (Sandbox Code Playgroud)

编辑:

[abigail@localhost my_env]$ sudo yum install python3-tk
[sudo] password for abigail: 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.supremebytes.com
 * epel: mirrors.kernel.org
 * extras: mirror.supremebytes.com
 * ius: mirrors.kernel.org
 * nux-dextop: li.nux.ro
 * rpmfusion-free-updates: mirror.web-ster.com
 * rpmfusion-nonfree-updates: mirror.web-ster.com
 * updates: mirror.supremebytes.com
No package python3-tk available.
Error: Nothing to do
Run Code Online (Sandbox Code Playgroud)

python3-tk 在 CentOS 7 上不可用?

Ser*_*aev 5

使用%paste需要安装python3-tk。我猜你误解了%cpaste工作原理。运行它,粘贴您的代码,按 Enter,输入--,再次按 Enter。


Bla*_*g23 5

如果您使用%cpaste,则必须键入%cpaste,然后ctrl+shift+v进行粘贴,然后键入--,然后enter退出粘贴的文本屏幕。任何stdout应该出现在这之后。

例子:

In [2]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:class Duck:
    def quack(self):
        print("Quaaaaaack!")
    def feathers(self):
        print("The duck has white and gray feathers.")::::
:--

In [3]: d = Duck()

In [4]: d.quack()
Quaaaaaack!
Run Code Online (Sandbox Code Playgroud)