954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pasting multiple lines into Python shell

I just started messing with Python, and was having a great time with entering Tkinter GUI commands into the Python shell (ActivePython 2.5).
Its fun entering commands one at a time and seeing components appear real-time (instead of normal process of having to compile the whole thing first).

Anyway, I was wondering if there was a way to paste multiple lines of source code (at once).

Also: Is there a function that causes a string to be interpreted as a command?

DustinS
Newbie Poster
11 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

I just started messing with Python, and was having a great time with entering Tkinter GUI commands into the Python shell (ActivePython 2.5). Its fun entering commands one at a time and seeing components appear real-time (instead of normal process of having to compile the whole thing first).

Anyway, I was wondering if there was a way to paste multiple lines of source code (at once).

Also: Is there a function that causes a string to be interpreted as a command?

I suggest the standard IDLE instead. I found ActivePython lacking in features (dedent region, indent region, comment region, uncomment region) yet containing no new (useful) ones.

As for pasting multiple lines: well, you can paste multiple lines, but it only seems to evaluate the first one.

What you can instead do is nest those multiple lines in a function, and then paste it.

def test():
  line1 = 'bah'
  line2 = 'foo'
  print line2 + line1


now type test() for the result 'foobah'

whereas

line1 = 'bah'
line2 = 'foo'
print line2 + line1

would only evaluate line1 = 'bah'

This is only marginally easier than creating an actual script, since you get to avoid creating a file just for a test.

Re using strings as commands... what's the context?

Oh, and I highly recommend wxPython over Tkinter. Much more powerful, intuitive and easy to use, IMHO. Also newer: http://www.wxpython.org/quotes.php

Note the words by Eric S. Raymond and Guido van Rossum (Python creator).

Fuse
Light Poster
46 posts since Jun 2008
Reputation Points: 27
Solved Threads: 3
 

The Python function eval() will take a string and try to evaluate/interpret it.

BTW, the PyScripter IDE allows you to interpret your source code without having to create an official file. I hate the Python shell, it's okay for one liners only!

If I were stranded on an island and were only allowed one Python GU toolkit, I would take wxPython. Actually, Fuse left a real nice introduction to wxPython programming here:
http://www.daniweb.com/forums/post621707-8.html

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

I tried eval('a=1') but it didn't work. I looked up the syntax for eval and discovered that exec was the function I was looking for.

>BTW, the PyScripter IDE allows you to interpret your source code without having to create an official file. I hate the Python shell, it's okay for one liners only!

Thanks!

Is there a way to run a segment of source code, then open a shell that has access to (and can change) the same variables and functions that were defined in the script?

DustinS
Newbie Poster
11 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

>As for pasting multiple lines: well, you can paste multiple lines, but it only seems to evaluate the first one.

>What you can instead do is nest those multiple lines in a function, and then paste it.

>

def test():
  line1 = 'bah'
  line2 = 'foo'
  print line2 + line1

Thanks for the tip.

DustinS
Newbie Poster
11 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

There is a little IDE called PyPE that allows you to run selected code in the shell. It has its own more advanced shell window.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 
There is a little IDE called PyPE that allows you to run selected code in the shell. It has its own more advanced shell window.

Thanks!

DustinS
Newbie Poster
11 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You