2

Sometimes, when developing python code, I open a script in gedit and copy chunks of code into the python command line on Linux. This used to work just fine. For example, here are two commands which I copied together:

import numpy as np import matplotlib.pyplot as plt 

Today, however, I am getting the following error when I try to copy and execute multiple lines together:

SyntaxError: multiple statements found while compiling a single statement 

This did not used to happen. Another difference is that my commands are highlighted white when I paste them into python. I feel like I must be missing something obvious. Any idea why I can't execute multiple lines together?

1
  • Did you used to be running your interactive shell within an IDE (or a different IDE, if you're still using one)? Some fancier interactive shells will split up your inputs and run one line at a time. Commented Feb 22, 2021 at 19:11

2 Answers 2

2

This smells like it has something to do with "bracketed paste mode", especially with that highlighting going on. From a distance I can't tell what or how, though. Possibly related to this issue: https://bugs.python.org/issue42819 .

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This seems to have been the issue. The page github.com/Homebrew/homebrew-core/issues/68193 suggested the workaround command: 'echo "set enable-bracketed-paste off" >> ~/.inputrc'. This resolved the problem and gave me the same python behavior as before, where I can copy chunks of code into the python terminal. However, one page (thejh.net/misc/website-terminal-copy-paste) suggested that bracketed paste mode is safer when copying unknown commands, so maybe I shouldn't turn it off? Does anyone know? I'm on python 3.8.5, and it sounds this may be fixed in newer versions.
0

When you are trying to run code in the Python shell, you can only run one line of code (more specifically, one statement) at a time. You would have to execute each import statement individually.

1 Comment

Hmm, this behavior is new to me. It seems like a change was made regarding "bracketed paste mode", which is mentioned in the answer that I accepted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.