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

时间:2016-11-22 22:42:35

标签: python-3.x ipython

我使用的是最新的ipython 5.1和python 3,但在将多行直接粘贴到命令行时遇到了麻烦。我正在研究CentOs。

有人可以尝试在维基(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.")

所有这些都是正确缩进的,可以粘贴到我的.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

EDITTED:

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

[abigail@localhost my_env]$ rpm -q tkinter
tkinter-2.7.5-39.el7_2.x86_64

但%粘贴始终显示错误:

In [10]: %paste
ERROR:root:Getting text from the clipboard on this platform requires Tkinter.

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

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

In [9]: 

EDITTED:

[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

python3-tk在CentOS 7上不可用?

3 个答案:

答案 0 :(得分:2)

使用%paste时需要安装python3-tk。我想你误解了%cpaste是如何工作的。运行它,粘贴代码,按Enter,输入--,再次按Enter键。

答案 1 :(得分:0)

如果使用%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!

答案 2 :(得分:0)

现在可以使用%alias_magic魔术命令设置便捷的快捷方式

键入%alias_magic -l p paste

从现在开始,将所需的行复制到剪贴板,然后只需按p <Enter>

我已经对您的课程进行了测试。它是一种魅力。

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

之后

 in [18]:d = Duck()
 in [19]:d.quack()
 Quaaaaaaaack