开发者

Copy-paste into Python interactive interpreter and indentation

开发者 https://www.devze.com 2023-04-12 12:02 出处:网络
This piece of code, file test.py, if 1: print \"foo\" print \"bar\" can be successfully executed with execfile(\"test.py\") or python test.py, but when one tries to copy-paste it into a Python inte

This piece of code, file test.py,

if 1:
   print "foo"
print "bar"

can be successfully executed with execfile("test.py") or python test.py, but when one tries to copy-paste it into a Python interpreter:

File "<stdin>", line 3
print "bar"
        ^
SyntaxError: invalid syntax

Why is it so? Can the interpreter by configured in such a way that it would read copy-pasted text successfully?

I guess that may affect typing in the interpreter, but th开发者_如何学Cat's OK for me.


Indentation is probably lost or broken.

Have a look at IPython -- it's an enhanced Python interpreter with many convenient features. One of them is a magic function %paste that allows you to paste multiple lines of code.

It also has tab-completion, auto-indentation... and many more. Have a look at their site.


Using %paste in IPython:

Copy-paste into Python interactive interpreter and indentation

And copy-and-paste stuff is one of the things fixed in the Qt console. Here's using a plain old copy-and-paste of your code block that "just works" in the new IPython qtconsole:

Copy-paste into Python interactive interpreter and indentation


I don't know any trick for the standard command prompt, but I can suggest you a more advanced interpreter like IPython that has a special syntax for multi-line paste:

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:for c in range(3):
:    print c
:
:--
0
1
2

Another option is the bpython interpreter that has an automatic paste mode (if you are typing too fast to be an human):

>>> for c in range(3):
...     print c
... 
0
1
2
>>> 
 <C-r> Rewind  <C-s> Save  <F8> Pastebin  <F9> Pager  <F2> Show Source 


Do %autoindent to make automatic indentation off. After that, you can paste your code in IPython.


Continuation lines are needed when entering a multi-line construct. --Interactive mode, The Python Tutorial (v2) (v3)

So you need to enter:

if 1:
   print "foo"

print "bar"

I've yet to find a suitable explanation as to why it's different to a non-interactive session, alas.


All of the current answers suggest you change to IPython. For a Python-only solution, you can use textwrap to remove leading whitespace from lines.

For example,

>>> code="""    x='your pasted code'
                y='with common indentation'"""
>>> formatted=textwrap.dedent(code)
>>> exec(formatted)


If you are like me and use Notepad++ (to copy and paste from), try to replace tabs by spaces by going to menu SettingsPreferencesLanguage and check the replace by spaces.

I had this problem myself for so long and I found out that python.exe recognizes spaces.


If the pasted content has any empty lines, the interpreter triggers evaluation when encountering them. If any line after an empty line has indentation, it will cause an IndentationError since any previous context has been closed.

Solutions:

  • Delete any empty lines before copying to clipboard.
  • Add any amount of indentation to empty lines (doesn't need to match code) before copying to clipboard.

Note that spaces vs. tabs does not seem to matter.


exec(pyperclip.paste())

provided you don't mind using exec. Any other third party clipboard package than pyperclip will do I guess.


One other solution I recently found for a similar problem:

$ python << EOF
if 1:
   print "foo"
print "bar"

EOF
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号