python 将多行粘贴到 IDLE 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1615379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:42:46  来源:igfitidea点击:

Pasting multiple lines into IDLE

pythonpython-idle

提问by foosion

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.

有没有办法将代码块粘贴到 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

回答by RedGlyph

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.

然后exec(cmds)将执行它们。

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.

这只是一个技巧,也许有更官方、更优雅的方式。

回答by Roger

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

IdleX为 IDLE 提供了 PastePyShell.py 扩展,它允许将多行粘贴到 shell 中以供执行。

回答by aless80

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

请参阅另一篇文章:Python,在 IDLE 中编写多行代码您可以使用编辑器(文件 > 新建文件),在那里编写代码行并使用 F5