将多行粘贴到 IDLE 中
有没有办法将代码块粘贴到 IDLE 中?逐行粘贴是可行的,但有时我想一次粘贴多行。当我尝试时,IDLE 会读取第一行并忽略其余行。
>>> 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能不是最漂亮的过程,但这是可行的:
粘贴您的命令,然后添加
'''
:然后
exec(cmds)
将执行它们。或者更直接,
然后粘贴你的命令,后面加上
''')
:这只是一个技巧,也许有更官方、更优雅的方式。
Probably not the most beautiful procedure, but this works:
paste your commands, followed by
'''
:Then
exec(cmds)
will execute them.Or more directly,
then paste your commands, followed by
''')
:It's just a trick, maybe there's a more official, elegant way.
IdleX 为 IDLE 提供 PastePyShell.py 扩展,允许将多行粘贴到 shell 中执行。
IdleX provides the PastePyShell.py extension for IDLE which allows pasting of multiple lines into the shell for execution.
请参阅另一篇文章:Python,在 IDLE 中编写多行代码
您可以使用编辑器(文件 > 新文件),在那里编写代码行并使用 F5
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
基于 RedGlyph 的答案,但使用 AutoHotkey (AHK) 进行一些自动化:
您需要安装 AutoHotkey 即可实现此功能。
安装 AutoHotkey 后,上述代码示例必须保存在扩展名为 AHK 的文件中(例如,Idle.ahk),并且应始终运行以启用快捷方式:
Ctrl + Shift + X 为您进行字符串操作。
Based on the answer of RedGlyph, but with some automation using AutoHotkey (AHK):
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.