Pasting multiple lines into IDLE

一个人想着一个人 提交于 2019-11-26 20:32:53

问题


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:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/1615379/pasting-multiple-lines-into-idle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!