user3213857
user3213857

Reputation: 399

SyntaxError: multiple statements found while compiling a single statement

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?

Screenshot:

screenshot

Upvotes: 28

Views: 199081

Answers (7)

bittahProfessional
bittahProfessional

Reputation: 204

I am on windows executing python code through wsl. What worked for me is switching to Jupyter Notebook. It's easy to get with pip install jupyter; then deploy with jupyter notebook. It allows pasting multiple lines into each 'cell' as well as plenty more functionality: using Jupyter Notebook

Upvotes: 1

Adnan Boz
Adnan Boz

Reputation: 824

I had the same problem. This worked for me on mac (and linux):

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

Upvotes: 67

mkrieger1
mkrieger1

Reputation: 23125

The solution I found was to download Idlex and use its IDLE version, which allows multiple lines.


This was originally added to Revision 4 of the question.

Upvotes: 1

Jason
Jason

Reputation: 87

You are using the interactive shell which allows on line at a time. What you can do is put a semi-colon between every line, like this - import sklearn as sk;import numpy as np;import matplotlib.pyplot as plt. Or you can create a new file by control+n where you will get the normal idle. Don't forget to save that file before running. To save - control+s. And then run it from the above menu bar - run > run module.

Upvotes: 2

Christabella Irwanto
Christabella Irwanto

Reputation: 1201

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

Upvotes: 1

Evgeni Sergeev
Evgeni Sergeev

Reputation: 23573

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.

Upvotes: 7

aIKid
aIKid

Reputation: 28232

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.

Upvotes: 22

Related Questions