This piece of code, file test.py,
if 1: print \"foo\" print \"bar\"
can be successfully executed with execfile(\"test.py\
execfile(\"test.py\
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)