When using magic %paste in ipython, it executes pasted code, rather than just pasting. How can i get it to just paste the copied code so that it can be edited?
Asked
Active
Viewed 3,316 times
3 Answers
6
You have two options:
- To edit it by hand, run
%cpaste
. Then you can paste it in with standard terminal options (try Ctrl-Shift-V), and edit it. Enter--
on a line to finish. - To change it as text in your code, run
%paste foo
. It will store the clipboard contents infoo
.

Thomas K
- 39,200
- 7
- 84
- 86
4
Adding to Thomas K's answer (quoted below), if you have stored statements to a string variable foo
by using %paste foo
, you can later run that string (or any python statements in string form) using the exec(foo [, globals, locals])
.
You have two options:
- To edit it by hand, run
%cpaste
. Then you can paste it in with standard terminal options (try Ctrl-Shift-V), and edit it. Enter--
on a line to finish.- To change it as text in your code, run
%paste foo
. It will store the clipboard contents infoo
.

Scott H
- 2,644
- 1
- 24
- 28
3
There is a solution for this issue in ipython, if you are not concerned with indentation,
Just run %autoindent
to Automatic indentation OFF.

Awais
- 1,803
- 22
- 26