我使用的是最新的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
在CentOS 7上没有python3-tk?
要使用%paste
,您需要安装python3-tk。而且我猜你误解了%cpaste
是如何工作的。运行它,粘贴代码,按Enter键,输入--
,再次按Enter键。
如果你使用%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!