python - SyntaxError: multiple statements found while compiling a single statement

python – SyntaxError: multiple statements found while compiling a single statement

python – SyntaxError: multiple statements found while compiling a single statement

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

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

In the shell, you cant 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 youre seeing a script, which will be executed later. But in the interactive interpreter, you cant do more than one statement at a time.

python – SyntaxError: multiple statements found while compiling a single statement

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, wont work for everything you might want to do, etc. But then, going to ipython / jupyter qtconsole is probably the next simplest option.

Related posts on python Syntax Error :

Leave a Reply

Your email address will not be published. Required fields are marked *