This piece of code, file test.py,
if 1:
print \"foo\"
print \"bar\"
can be successfully executed with execfile(\"test.py\
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:
Note that spaces vs. tabs does not seem to matter.
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.