Python Forum
How to paste several lines of codes to the Python console
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to paste several lines of codes to the Python console
#1
I'm not sure in which part of the forum to post this (as this is not really code related).

I used to be able to do this until very recently. Now when I try to paste several lines at once I get the error message:
Quote:SyntaxError: multiple statements found while compiling a single statement
My current version of Python is 3.9.0 which might be the version where this behaviour changed. I tried to search the internet about this and it looked like you were never meant to be able to paste several lines at once on the console...?

Being able to paste several lines at once is quite convenient when testing ideas and code snippets, especially since if anything goes wrong you get to investigate directly your variables etc. I feel like I really need this feature, pasting line by line is very time consuming and using trick such as putting everything in a big string and calling exec also feels cumbersome.

Is there some configuration to change this? Is this version related?

For a little of context: I'm using Python on Linux.
Reply
#2
I move code around all of the time.
I use cut and paste sometimes, but usually keep related classes in the same module, so only have to move the module, then import what I need.
I use VSCode for an IDE, and have never had an issue with cut and paste.
what are you using for editing? on what platform?
Reply
#3
For editing, I'm using old fashioned text editor with an opened console on a separate windows (and also Jupyter sometimes, but the issue doesn't apply here). I've tried PyCharm but it was extremely slow so I reverted back to simple text editor.

I also import some of the code, including using the reload command from the import library, but still use the copy-paste mechanism a lot.

Add.: The implementation of Python that is installed on the system is the CPython one.
Reply
#4
Do I understand correctly that we are talking about Python interactive interpreter and pasting multiline code there?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Quote:Do I understand correctly that we are talking about Python interactive interpreter and pasting multiline code there?

Yes
Reply
#6
I tried multiply line paste in Python 3.9 and it worked as expected. Error message seems to imply that there are multiple statements found in one statement. It wouldn't hurt to check syntax and maybe there is some error which makes Python consider multiple statements as one.

If you already paste code then you could do into file instead of interactive interpreter and put breakpoint() into needed places. This way it is possible to run file but also 'investigate directly'.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
This happen for any code i'd paste, even the most trivial one, so I don't think it's a problem with the syntax, for example:
print('a')
i = 1
doesn't work. The weird thing is:
- copypasting with an empty line at the beginning gives the error (which is what usually happens when i select code)
- copypasting with no empty line only execute the first line (i is not defined, also apparent when pasting two "print" instructions)

When pasted, the code is highlighted and i need to press enter, it wasn't like that when I used to copy paste several lines and it worked (I've been doing this for a long time before that, including or not including an empty line in the paste, all the code was executed and not just the first line, also the pasted code was not highlighted as it is now)

I'll try the breakpoint() thing for debugging, thanks for the suggestion.

Added: so maybe that's just either some configuration or a compilation option that changed for the version available on my Linux distribution?
Reply
#8
"When pasted, the code is highlighted and i need to press enter"

Highlighting indicates that some settings are applied. I have had paste/highlight issue in shell (not in Python interactive interpretator) and if I remember correctly it was related to bashrc, inputrc and terminal settings.

Did it work initially under 3.9? If it worked then it indicates that some settings were changed. If it worked in previous Python version but not under 3.9 you can check whether paths are similar and if not there might be environment variables applied differently.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
I pasted on Linux (mint 20) python 3.9.0, worked fine:
Python 3.9.0 (default, Oct  6 2020, 22:09:03) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('a')
a
>>> i = 1
>>>
Reply
#10
Quote:Highlighting indicates that some settings are applied. I have had paste/highlight issue in shell (not in Python interactive interpretator) and if I remember correctly it was related to bashrc, inputrc and terminal settings.

Did it work initially under 3.9? If it worked then it indicates that some settings were changed. If it worked in previous Python version but not under 3.9 you can check whether paths are similar and if not there might be environment variables applied differently.

It used to worked until recently (maybe a week ago) and I've been using Python for years. So maybe as you suggest some configuration changes occurred during transition from a previous version of Python. The thing is I'm not entirely sure that this behaviour changed exactly when I changed version, I only realised this while using Python and I update the system very regularly.

The mention of bashrc and terminal settings reminds that I have also installed miniconda recently which could also have interfered with the configuration?

I'll try to have a look on the terminal configuration. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with Spyder IDE: Clear console doesn't actually clear the console GalacticStarfish 2 5,548 Sep-03-2017, 09:01 PM
Last Post: dvs1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020