I think this is exactly what %cpaste
is for (I am always forgetting about all the things IPython does). %cpaste
enters a state allowing you to paste already formatted or indented code, and it will strip leading indentation and prompts, so you can copy/paste indented code from files, or even from an interactive Python session including leading >>>
or In [1]
which will be stripped.