10

Many online python examples show interactive python sessions with normal leading ">>>" and "..." characters before each line.

Often, there's no way to copy this code without also getting these prefixes.

In these cases, if I want to re-paste this code into my own python interpreter after copying, I have to do some work to first strip off those prefixes.

Does anyone know of a way to get python or iPython (or any other python interpreter) to automatically ignore leading ">>>" and "..." characters on lines that are pasted in?

Example:

>>> if True:
...     print("x")
... 
tdelaney
  • 73,364
  • 6
  • 83
  • 116
HippoMan
  • 2,119
  • 2
  • 25
  • 48
  • 1
    @PadraicCunningham, it is _not_ the default behaviour in the `python` interpreter, though. [IPython](https://ipython.org/) is not included with Python. – ChrisGPT was on strike Jan 18 '16 at 18:14
  • 3
    Yeah but the op _did_ mention IPython in the question :) – bakkal Jan 18 '16 at 18:14
  • 1
    @Chris, I never mentioned the python interpreter, I talked about the ipython interpreter referencing the OP, *Does anyone know of a way to get python or **iPython**...* – Padraic Cunningham Jan 18 '16 at 18:16
  • 1
    Fair point bakkal and Padraic. I missed that. – ChrisGPT was on strike Jan 18 '16 at 18:18
  • 1
    @HippoMan - could you give us an example to test so that we are all on the same page? A multiline for loop would be interesting. I cut/pasted from a regular python session to ipython and it didn't work. – tdelaney Jan 18 '16 at 18:18
  • In the Python docs, you can click the button in the top right corner of the code box and it'll remove them. – Blender Jan 18 '16 at 18:21
  • IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: >>> if True: ...: ... print("x") ...: ... ...: File "", line 2 ... print("x") ^ SyntaxError: invalid syntax – HippoMan Jan 18 '16 at 18:23
  • OOPS! Markdown didn't give me newlines. Anyway, the code that didn't work in iPython was symply to paste the following into it. First line: '>>> if True:'; second line: '... print("x")' with 5 spaces after the "..." – HippoMan Jan 18 '16 at 18:24

3 Answers3

5

IPython will do this for you automatically.

In [5]: >>> print("hello")
hello

In [10]: >>> print(
   ....: ... "hello"
   ....: )
hello
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I tried a `for` loop (the multiline equivalent of `for i in range(10):pass`) and it didn't work. I got a SystaxError on "pass". – tdelaney Jan 18 '16 at 18:16
  • Well IPython will try to indent for you automatically, so that's probably the problem there. You can do `%cpaste` to activate paste mode, then do `--` to finish pasting. – Daniel Roseman Jan 18 '16 at 18:20
3

You just need to either switch off autoindent to include >>> and ... in a multiline paste:

In [14]: %autoindent
Automatic indentation is: OFF
In [15]: >>> for i in range(10):
   ....: ...     pass
   ....: 

In [16]: >>> for i in range(10):
   ...: ...     pass
   ...: ... 
In [17]: >>> for i in range(10):
   ...: ...     pass
   ...: ... 

In [18]: %autoindent
Automatic indentation is: ON

In [19]: >>> for i in range(10):
   ....:     ...     pass
   ....:     
  File "<ipython-input-17-5a70fbf9a5a4>", line 2
    ...     pass
    ^
SyntaxError: invalid syntax

Or don't copy the >>> and it will work fine:

In [20]: %autoindent
Automatic indentation is: OFF

In [20]:  for i in range(10):
   ....: ...     pass
   ....: 
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Yes. It works for me in ipython, now that I turned %autoindent off. That's sufficient for solving my problem. Thanks to all. – HippoMan Jan 18 '16 at 18:34
2

Not quite the same as pasting into the shell, but the doctest module can be useful. It scans a python module or regular text file looking for interactive script fragments and then runs them. Its primary use case is to blend documentation and unit test. Suppose you have a tutorial such as

This is some code to demonstrate the power of the `if`
statement. 

>>> if True:
...     print("x")
... 
x

Remember, each `if` increases entropy in the universe,
so use with care.

>>> if False:
...     print("y")
... 

Save it to a file and then run doctest

$ python -m doctest -v k.txt
Trying:
    if True:
        print("x")
Expecting:
    x
ok
Trying:
    if False:
        print("y")
Expecting nothing
ok
1 items passed all tests:
   2 tests in k.txt
2 tests in 1 items.
2 passed and 0 failed.
Test passed.

doctest runs the script fragments and compares it to the expected output.

UPDATE

Here's a script that will take what's in the clipboard and paste back the python script fragments. Copy your example, run this script and then paste into the shell.

#!/usr/bin/env python3

import os
import pyperclip

pyperclip.copy(os.linesep.join(line[4:] 
    for line in pyperclip.paste().split(os.linesep)
    if line[:4] in ('>>> ', '... ')))
tdelaney
  • 73,364
  • 6
  • 83
  • 116