我曾经遇到过同样的问题。这个方法在我的 Mac(和 Linux)上起作用:
echo "set enable-bracketed-paste off" >> ~/.inputrc
在 shell 中,您无法一次执行多个语句:
>>> x = 5
y = 6
SyntaxError: multiple statements found while compiling a single statement
您需要逐个执行它们:
>>> x = 5
>>> y = 6
>>>
当你看到多个声明被列出时,那意味着你正在看一个脚本,它将在以后执行。但在交互式解释器中,你一次只能执行一个语句。
x=6 \n x=6
就可以正常工作。如果没有完整的回溯信息,很难说清楚,我认为实际答案更接近 jmu 的看法。 - Games Brainiac一个(部分的)实践解决方法是将东西放入一个可丢弃的函数中。
粘贴
x = 1
x += 1
print(x)
导致
>>> x = 1
x += 1
print(x)
File "<stdin>", line 1
x += 1
print(x)
^
SyntaxError: multiple statements found while compiling a single statement
>>>
然而,粘贴
def abc():
x = 1
x += 1
print(x)
工作:
>>> def abc():
x = 1
x += 1
print(x)
>>> abc()
2
>>>
ipython
/jupyter qtconsole
可能是下一个最简单的选项。您正在使用交互式shell,每次只能一行输入。您可以在每行之间加上分号,像这样- import sklearn as sk;import numpy as np;import matplotlib.pyplot as plt
。或者您可以通过控制+N创建一个新文件,在那里您将获得正常的闲置状态。运行之前不要忘记保存该文件- 控制+S。然后从上面的菜单栏运行- 运行>运行模块。