Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pasting multiple lines into IDLE

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 
like image 716
foosion Avatar asked Oct 23 '09 19:10

foosion


People also ask

How do you enter multiple line codes in IDLE?

If you do File --> New File, it should open a new savable window that you can write multiple lines and save as a . py file.

How do I paste multiple lines of code in Python shell?

using pyscripter.. copy code from anywhere say a function... and then right click in interpreter... choose "paste and execute". and this will work nicely for multiline paste.

What is multi lining in Python?

PythonServer Side ProgrammingProgramming. Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example − total = item_one + \ item_two + \ item_three.


1 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.

like image 166
RedGlyph Avatar answered Sep 20 '22 23:09

RedGlyph