views:

137

answers:

1

Is there a way to paste a block of code into IDLE? Pasting line by line works, but sometimes I'd like to paste many lines at once. When I try, IDLE reads the first line and ignores the rest.

>>> a = 1
b = 2
c = 3

>>> 
>>> a
1
>>> b

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    b
NameError: name 'b' is not defined
+1  A: 

Probably not the most beautiful procedure, but this works:

cmds = '''

paste your commands, followed by ''':

a = 1
b = 2
c = 3
'''

Then exec(cmds) will execute them.

Or more directly,

exec('''

then paste your commands, followed by '''):

a = 1
b = 2
c = 3
''')

It's just a trick, maybe there's a more official, elegant way.

RedGlyph
that works, but I was really hoping for something more elegant. It's pretty common to paste a bunch of lines into IDLE. Testing parts of code from an IDE or running stuff posted on SO or whatever.
foosion
Yes, I often ran into the same issue and asked myself the same question... Same happens when pasting indented part of code, "solved" by typing `if True:` then pasting the code. A bit of a dirty trick ;-)
RedGlyph