开发者

Pasting multiple lines into IDLE

开发者 https://www.devze.com 2022-12-09 02:16 出处:网络
Is there a way to paste a block of cod开发者_运维技巧e 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

Is there a way to paste a block of cod开发者_运维技巧e 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


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号