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?
IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. from __future__ import print_function import sys. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.
Notebook 1: — Select multiple cells by holding down Shift and hit Ctrl+c to copy. Notebook 2: — Hit Esc to enter Command mode Ctrl + v to paste.
The magic commands, or magics, are handy commands built into the IPython kernel that make it easy to perform particular tasks, for example, interacting Python's capabilities with the operating system, another programming language, or a kernel. IPython provides two categories of magics: line magics and cell magics.
You have two options:
%cpaste
. Then you can paste it in with standard terminal options (try Ctrl-Shift-V), and edit it. Enter --
on a line to finish.%paste foo
. It will store the clipboard contents in foo
.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
.
There is a solution for this issue in ipython, if you are not concerned with indentation,
Just run %autoindent
to Automatic indentation OFF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With