How does IPython's magic %paste work?

PythonIpython

Python Problem Overview


I want to copy already indented Python code / whole functions and classes into IPython. Everytime I try the indentation is screwed up and I get following error message:

IndentationError: unindent does not match any outer indentation level (<ipython-input-23-354f8c8be51b>, line 12)

If you want to paste code into IPython, try the %paste and %cpaste magic functions.

Python Solutions


Solution 1 - Python

You can't copy to IPython directly. This are the steps:

  1. Copy the lines you want to copy into IPython into the clipboard
  2. Enter %paste into IPython
  3. Press enter
  4. Profit!

Solution 2 - Python

A clarification on the steps:

  • First, copy target lines into your clipboard.

  • Type into the iPython prompt:

    • If on Tkinter: enter %paste
    • Otherwise: enter %cpaste
  • Paste (Ctrl-V) and hit enter.

  • Then type -- and hit enter.

For example:

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:for l in "Hello World":
:    print l,
:--
H e l l o   W o r l d

Solution 3 - Python

As of Ipython 5 you don't need any magic command, just paste it

ipython5

> Thanks to prompt_toolkit, IPython now supports:

> - Syntax highlighting as you type

  • Real multi-line editing (up and down arrow keys move between lines)
  • Multi-line paste without breaking indentation or immediately executing code
  • Better code completion interface (we plan to improve that more) Optional mouse support

More on this here

To upgrade ipython to the latest version

pip install ipython --upgrade 

Solution 4 - Python

%paste requires Tkinter. If you are in ubuntu, you can install it by

sudo apt-get install python-tk

If you are on Python3

sudo apt-get install python3-tk

Then restart ipython and use %paste to paste from your clipboard.

Solution 5 - Python

One of the useful answers was lost in the comments, so wanted to restate it along with adding a reference for another useful IPython magic function.

First to restate what @EOL said, one way to solve OP's problem is to turn off auto-indentation by first running %autoindent and doing the paste (not needed if you are using %paste, of course).

Now to add more information to what is already there here, one more useful mode in IPython is %doctest_mode which allows you to copy paste example and test snippets from doc strings. This is also useful to execute interactive python session output that you could find in documentation and online forums, without having to first strip out the prompt strings.

Solution 6 - Python

For ubuntu users who are on Python 3.

The python-tk is for Python 2.

To make %paste work on Python 3, install the python3-tk package:

sudo apt-get install python3-tk

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFramesterView Question on Stackoverflow
Solution 1 - PythonFramesterView Answer on Stackoverflow
Solution 2 - PythonYulyView Answer on Stackoverflow
Solution 3 - PythonLevonView Answer on Stackoverflow
Solution 4 - PythonChillar AnandView Answer on Stackoverflow
Solution 5 - PythonharidsvView Answer on Stackoverflow
Solution 6 - PythonutapyngoView Answer on Stackoverflow