Copy-paste into Python interactive interpreter and indentation

前端 未结 8 1686
借酒劲吻你
借酒劲吻你 2020-12-06 00:41

This piece of code, file test.py,

if 1:
   print \"foo\"
print \"bar\"

can be successfully executed with execfile(\"test.py\

8条回答
  •  难免孤独
    2020-12-06 00:59

    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

提交回复
热议问题