syntaxerror when pasting multiple lines in python [duplicate]

  • Last Update :
  • Techknowledgy :

Short answer:

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

Suggestion : 2

This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide.

DISCLAIMER: This is the first time I submit an issue here. In advance, my humble apologies if I missed something.
Feel free to correct me :)

--

I regularly test snippets of code by pasting them from a code editor to a shell REPL.

It works perfectly well in python 3.8 or 3.7 but not in python 3.9.

Demonstration:

Try to copy and paste the following simple snippet:

---

def f():
print("hello world")

---

The result in a python 3.8 REPL (same with 3.7):

---

$ python3.8
Python 3.8.6 (default, Nov 20 2020, 18:29:40)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
print("hello world")
>>> f()
hello world

---

But with python 3.9:

---

$ python3.9
Python 3.9.1 (default, Dec 10 2020, 10:36:35)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
print("hello world")
File "<stdin>", line 1

   ^
   SyntaxError: multiple statements found while compiling a single statement

   ---

   This behavior happens with any snippet of code containing at least one indented line, whether by tabs or spaces and whatever the number of spaces.


   Regards.
Sorry, I cannot reproduce that behavior.The output you show isn 't what I would expect, in any case.

$ python3 .8
Python 3.8 .7(v3 .8 .7: 6503 f05dd5, Dec 21 2020, 12: 45: 15)[Clang 6.0(clang - 600.0 .57)] on darwin
Type "help", "copyright", "credits"
or "license"
for more information. >>>
   def f():
   ...print("hello world")
   ...
   >>>
   ^ D
$ python3 .9
Python 3.9 .1(v3 .9 .1: 1e5 d33e9b9, Dec 7 2020, 12: 44: 01)[Clang 12.0 .0(clang - 1200.0 .32 .27)] on darwin
Type "help", "copyright", "credits"
or "license"
for more information. >>>
   def f():
   ...print("hello world")
   ...
   >>>
   ^ D

Note the missing '...'
continuation prompts.So can you verify that the text when you paste it includes just a standard linefeed(LF) control character as the end - of - line delimiter ? Other possible differences might be whether the python in use was linked with the BSD libedit library or with GNU readline but that shouldn 't make a difference unless you have some non-default options in their configuration files.  You can check which one is in use with this:

$ python3 .8 - c "import readline;print(readline.__doc__)"
Importing this module enables command line editing using GNU readline.
$ python3 .9 - c "import readline;print(readline.__doc__)"
Importing this module enables command line editing using libedit readline.

If all
else fails, from where did you obtain the pythons that you are using ? And in what environment are you running those commands, i.e.the macOS Terminal.app ?
On both Windows and macOS Mohave with both 3.9 and 3.10 I get normal behavior.

If it were not
for the $ python3.x line, the missing '... ' in Romain 's output would have suggested that he was using IDLE.  But IDLE should accept pasted ascii statements just fine.
The lack of dots was something I noticed.

So from your questions(Ned Deily) I have been testing out several things and found a "wae"!

   But first, to answer your questions:

   1. both LF and CRLF and it didn 't change anything.

2. Running "import readline;print(readline.__doc__)"
prints
   "... GNU readline", with python 3.7, 3.8 and 3.9.

3. I am using iTerm2, but the problem also happens on MacOS 's native Terminal.app. Versions of python were installed with **homebrew**.

Maybe worth to mention: if I paste my code in a multi line string to execute with python - c, then it works properly.

e.g.

   -- -

   python3 .9 - i - c 'a = 42
if a:
   print("hello world")
'
hello world
   >>>

   -- -

   The example is different because I realized I had the same problem on python3 .7 and python3 .8 when the 2 first lines were at the same level of indentation(Note sure
      if this gives a hint as to what the problem is).

HOWEVER,
if I use python versions directly downloaded from https: //www.python.org/, then I don't have the problem at all!

   Demonstration:

   -- -
   /Library/Frameworks / Python.framework / Versions / 3.7 / bin / python3 .7
Python 3.7 .2(v3 .7 .2: 9 a3ffc0492, Dec 24 2018, 02: 44: 43)[Clang 6.0(clang - 600.0 .57)] on darwin
Type "help", "copyright", "credits"
or "license"
for more information. >>>
   import readline;
print(readline.__doc__)
Importing this module enables command line editing using libedit readline. >>>
   a = 42 >>>
   if a:
   ...print("hello world")
   ...
   hello world >>>

   -- -

   Same
for python3 .9
Thank you
for retesting with the python.org installer.Since this is Homebrew specific, please open an issue with them, with the updated debug information.
It certainly could be an issue with using GNU readline vs libedit and Terry is correct that you should follow up with Homebrew.But I don 't think you stated from which code editor application you were copying from; that could also make a difference. In any case, good luck!

Suggestion : 3

The code is formatted as it would appear in the Python interpreter, and if you copy and paste this directly into IPython you get an error:,A command with a similar intent is %cpaste, which opens up an interactive multiline prompt in which you can paste one or more chunks of code to be executed in a batch:,These magic commands, like others we'll see, make available functionality that would be difficult or impossible in a standard Python interpreter.,In the direct paste, the interpreter is confused by the additional prompt characters. But never fear–IPython's %paste magic function is designed to handle this exact type of multi-line, marked-up input:

>>> def donothing(x):
   ...
   return x
In [2]: >>> def donothing(x):
...: ... return x
...:
File "<ipython-input-20-5a66c8964687>", line 2
   ... return x
   ^
   SyntaxError: invalid syntax
In[3]: % paste >>>
   def donothing(x):
   ...
   return x

# #--End pasted text--
In[4]: donothing(10)
Out[4]: 10
In[5]: % cpaste
Pasting code;
enter '--'
alone on the line to stop or use Ctrl - D.: >>> def donothing(x):
   : ...
   return x: --
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
# file: myscript.py

def square(x):
   ""
"square a number"
""
return x ** 2

for N in range(1, 4):
   print(N, "squared is", square(N))

Suggestion : 4

If you want to paste multiple lines in the terminal, it is recommended that you use %paste.,IPython is meant to work as a drop-in replacement for the standard interactive interpreter. As such, any code which is valid python should execute normally under IPython (cases where this is not true should be reported as bugs). It does, however, offer many features which are not available at a standard python prompt. What follows is a list of these.,IPython tells you that it has altered your command line by displaying the new command line preceded by --->.,You can also have multiple IPython instances in your program and open them separately, for example with different options for data presentation. If you close and open the same instance multiple times, its prompt counters simply continue from each execution to the next.

$ ipython[options] files
ipython--matplotlib qt
ipython--TerminalIPythonApp.matplotlib = 'qt'
In[8]: % cd /
   home / fperez
In[10]: % % timeit x = range(10000)
   ...: min(x)
   ...: max(x)
   ...:
   1000 loops, best of 3: 438 us per loop
In[9]: cd mydir /
   home / fperez / mydir