Pasting multiple lines into IDLE

Question:

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
Asked By: foosion

||

Answers:

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.

Answered By: RedGlyph

IdleX provides the PastePyShell.py extension for IDLE which allows pasting of multiple lines into the shell for execution.

Answered By: Roger

See this other post: Python, writing multi line code in IDLE
You can use an editor (File > New File), write your lines of code there and use F5

Answered By: aless80

Based on the answer of RedGlyph, but with some automation using AutoHotKey:

; python Idle shell
#IfWinActive ahk_exe pythonw.exe

^+x::  ; Idle - multiple commands paste 
SoundBeep,1700, 150
send, ^{end}
var1 = cmds = '''
var2 := clipboard
var3 = %var1%%var2%
var4 = %var3%'''
msgbox,,, %var4%,2
clipboard = %var4%
send, ^{vk0x56}   ;send, ^v
send, {enter}
sleep, 1000
send, exec(cmds)
return

You need to install AutoHotKey to make this work.
After Installing AutoHotKey, the above code sample must be saved in a file with extension AHK (e.g. Idle.ahk), and should be running always to enable the shortcut:
Ctrl+Shift+x to make the string manipulation for you.

Answered By: yosi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.