Copy-paste into Python interactive interpreter and indentation Copy-paste into Python interactive interpreter and indentation python python

Copy-paste into Python interactive interpreter and indentation


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:

Enter image description here

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:

Enter image description here


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]: %cpastePasting code; enter '--' alone on the line to stop.:for c in range(3)::    print c::--012

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... 012>>>  <C-r> Rewind  <C-s> Save  <F8> Pastebin  <F9> Pager  <F2> Show Source 


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.