Search code examples
pythonpython-3.xwindowspython-idle

Code pasting incorrectly into IDLE shell on Windows 10


I was trying to copy and paste some code into my Python shell, but when more than a single line in pasted in, I get the error: SyntaxError: multiple statements found while compiling a single statement.

I have a computer running Windows 10. I have always been annoyed that I couldn't copy-paste code and run it.

Is there any way to get all the code to separate into different statements? As if I had written it line-by-line / copy-pasted each line separately?

Here's what's happening:

screenshot showing error


Solution

  • The '>>> ' prompt in IDLE means "Enter one statement". Whenever you hit at the end of entered code, whether keyed or pasted, IDLE assumes that you have done just that and tries to compile your code as a single statement by calling compile with mode='single'. I have thought about how to handle multi-statement pastes, but it is a non-trivial problem.

    For the present, paste into an editor window, and run from there. I have a scratch file that I use for this purpose. I use it often enough that I usually can load it from File => Recent Files, hit ^A (select all), ^V (paste, on Windows), and F5.