8
votes

I'm in Python 3.3 and I'm only entering these 3 lines:

import sklearn as sk
import numpy as np
import matplotlib.pyplot as plt

I'm getting this error:

SyntaxError: multiple statements found while compiling a single statement

What could I be doing wrong?

Edit: If anyone comes across this question, the solution I found was to download Idlex and use its IDLE version, which allows multiple lines.

Screenshot: http://imgur.com/AJSrhhD

4
please put the complete traceback. - James
Could be a whitespace problem. I'm not sure if you're using the shell or a script, but have you tried creating a new file and rewriting? - jayelm
I'm using IDLE. There's no trackback error shown. It just says the error message I posted in the title. Is there something I should do to find it? I'm a noob, as is obvious now. - user3213857
@user3213857 Have you take a look at my answer? - aIKid
Post a screenshot, then. There should be more than what you're getting, and a screenshot may be the easiest way to figure out what's really happening. - user2357112 supports Monica

4 Answers

16
votes

In the shell, you can't execute more than one statement at a time:

>>> x = 5
y = 6
SyntaxError: multiple statements found while compiling a single statement

You need to execute them one by one:

>>> x = 5
>>> y = 6
>>>

When you see multiple statements are being declared, that means you're seeing a script, which will be executed later. But in the interactive interpreter, you can't do more than one statement at a time.

10
votes

I had the same problem. This worked for me on mac:

echo "set enable-bracketed-paste off" >> ~/.inputrc
2
votes

A (partial) practical work-around is to put things into a throw-away function.

Pasting

x = 1
x += 1
print(x)

results in

>>> x = 1
x += 1
print(x)
  File "<stdin>", line 1
    x += 1
print(x)

    ^
SyntaxError: multiple statements found while compiling a single statement
>>>

However, pasting

def abc():
  x = 1
  x += 1
  print(x)

works:

>>> def abc():
  x = 1
  x += 1
  print(x)
>>> abc()
2
>>>

Of course, this is OK for a quick one-off, won't work for everything you might want to do, etc. But then, going to ipython / jupyter qtconsole is probably the next simplest option.

1
votes

Long-term solution is to just use another GUI for running Python e.g. IDLE or M-x run-python in Emacs.