This piece of code, test.py:
if 1:
print "foo"
print "bar"
can be succesfully executed with execfile("test.py")
or python test.py
, but when one tries to copy-paste it into python interpreter:
File "<stdin>", line 3
print "bar"
^
SyntaxError: invalid syntax
Why is it so? Can interpreter by configured in such a way that it would read copy-pasted text succesfully? I guess that may affect typing in the interpreter, but that's ok for me.
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:
Another option is the bpython interpreter that has an automatic paste mode (if you are typing too fast to be an human):
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.
e.g.
Do
%autoindent
to Automatic indentation OFF. after that you can past your code in IPython.One other solution I recently found for similar problem:
If you are like me and use Notepad++ (to copy and paste from), try to replace tabs by spaces by going to settings>preferences>language and check the replace by spaces.
I had this problem myself for so long and I found out that python.exe recognizes spaces.
So you need to enter:
I've yet to find a suitable explanation as to why it's different to a non-interactive session, alas.