SyntaxError: multiple statements found while compiling a single statement
Asked Answered
S

7

27

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

Scotfree answered 20/1, 2014 at 5:27 Comment(0)
C
62

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

echo "set enable-bracketed-paste off" >> ~/.inputrc
Cultus answered 30/5, 2021 at 4:41 Comment(4)
For macOS, this is the correct answer. Just add that and restart the python console and it will now accept pasting multiple lines.Alexi
did not work for me on itermRemoved
See this homebrew issue for more details: github.com/Homebrew/homebrew-core/issues/68193Sappanwood
I am on Ubuntu Linux and it worked for me too. Thank you!Valera
F
23

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.

Frida answered 20/1, 2014 at 5:42 Comment(11)
This is not true, you can just paste in x=6 \n x=6 and it will work just fine. Its impossible to say without a full traceback, and I think the actual answer is more towards jmu'sTorticollis
@GamesBrainiac Hum.. Really? I pasted your snippet and i got SyntaxError: unexpected character after line continuation character..Frida
I pasted his code into the console, and got no error whatsoever.Torticollis
@Games What are you using? I pasted it and i got exactly the same error with the one in my answer.Frida
I believe GB means \n as in an actual newline. aIKid, are you pasting a literal backslash and n?Tetrabranchiate
@Tetrabranchiate Ah, yeah, i did. But using an actual newline just got me the same error as in my answer.Frida
Let's just continue in chat, meanwhile i'll delete this answer.Frida
@GamesBrainiac You're still here?Frida
@Frida mmhmm. I think we need to see a full traceback before we can actually give a definitive answer.Torticollis
@GamesBrainiac Yeah, sure, but can you join me in chat?Frida
@GamesBrainiac False. Using the shell you cannot enter multiple statements using Enter or \n. You can enter multiple statements by separating the statements using semicolon, as in x = 5; y = 6 . You need to go into script mode.Mcwherter
M
7

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.

Mozzarella answered 21/7, 2020 at 22:58 Comment(0)
S
2

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.

Suddenly answered 8/12, 2021 at 2:46 Comment(0)
P
1

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.

Pohl answered 20/1, 2014 at 5:27 Comment(0)
L
1

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

Levison answered 27/5, 2021 at 9:34 Comment(0)
A
1

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

Atronna answered 24/11, 2023 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.