1

I'm an experienced programmer getting started with Python. I've written a simple Python script that I've placed into a file called add_function.py:

def myadd(a, b):

    sum = a + b
    return sum

result = myadd(10, 15)
print result

Now, when I source the file from the Python interactive interpreter, it works fine:

% python
Python 2.7.5 (default, Sep 12 2013, 21:33:34) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile("add_function.py")
25

However, when copy the script and then paste it directly into the interpreter, it seems that the interpreter cannot parse the blank lines. I find this frustrating because other scripting languages (e.g. R) do not distinguish between blank lines in a script and blank lines at the interactive prompt.

>>> def myadd(a, b):
... 
  File "<stdin>", line 2

    ^
IndentationError: expected an indented block
>>>     sum = a + b
  File "<stdin>", line 1
    sum = a + b
    ^
IndentationError: unexpected indent
>>>     return sum
  File "<stdin>", line 1
    return sum
    ^
IndentationError: unexpected indent
>>> 
>>> result = myadd(10, 15)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'myadd' is not defined
>>> print result
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'result' is not defined

How do I fix this problem? I want to be able to paste in and try out code that I find on websites, and many of them have blank lines.

stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
  • 1
    Pasting random code you find on websites into python does not sound like a good idea, especially entire blocks of code. Also, https://github.com/dxa4481/Pastejacking. – melpomene Jun 17 '16 at 23:38
  • Python is a whitespace-significant language, and so is the python REPL. The REPL is not intended to be used the way you're trying to use it. Just paste the code into a file and run it from there. – Hamms Jun 17 '16 at 23:40
  • @melpomene: Of course I don't paste in random code. – stackoverflowuser2010 Jun 17 '16 at 23:52

2 Answers2

4

Use exec with a triple-quoted raw string:

exec r'''
[paste code here]
'''

If the pasted code uses triple-quotes, particularly for docstrings, you may need to check for that and use """ or ''' appropriately.

If the pasted code uses both types of triple-quote (rare but possible), you can get around that:

def heredoc(end='EOF'):
    lines = []
    while True:
        line = raw_input()
        if line == end:
            break
        lines.append(line)
    return '\n'.join(lines) + '\n'

Then you can do

>>> exec heredoc()
[paste code here]
EOF

where EOF is typed manually.

user2357112
  • 260,549
  • 28
  • 431
  • 505
2

Try ipython- its CLI has a specific paste mode: you just type %paste in the interpreter, and it will handle the pasted indents better. You can get ipython by simply using:

pip install ipython

Then starting your REPL interpreter by typing ipython / ipython3

tschundler
  • 2,098
  • 1
  • 14
  • 15