7

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?

dimo414
  • 47,227
  • 18
  • 148
  • 244
user1500448
  • 81
  • 1
  • 4

3 Answers3

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 in foo.
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 in foo.
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