O'Reilly Network     Published on the O'Reilly Network (http://www.oreillynet.com/)
    http://www.onlamp.com/pub/a/python/2001/10/11/pythonnews.html
    See this if you're having trouble printing code examples


Interactive Python

by Stephen Figgins
10/11/2001

I love the command line more than any graphic interface. I want to type commands, not move graphics around on a screen. Working through my keyboard is faster than clicking for me, and what I do seems more real. I don't know what it is that makes the keyboard seem more real than icons in a window. They're both abstractions, different ways of interacting with a a flow of bits and bytes. Maybe I was brainwashed from my early exposure to Unix.

Moving from Perl to Python, Python's intaractive prompt caught my attention right away. Demonstrating Python to my co-workers, I wowed them by creating Tk applications line by line. It was a demonstartion of prompt over GUI. Call it lexokinesis. Surely, creating an interface with a visual toolkit would be as easy or easier. You could draw one picture instead of writing a thousand words. Maybe it was our shared respect for the command line that made this ability seem useful in some way. As fledgling programmers, we liked that we could see more clearly what was going on.

The prompt very useful when learning Python. You enter commands and you get immediate feedback. I like that better than typing everything in at once then trying to run it--or more likely, trying to figure out why it didn't run. Using the prompt you explore as you go. When I was first learning Python, I even considered using it as my shell prompt. I eventually decided it took too much typing and gave up the idea. I must not have been inspired enough, or lazy enough. Nathan Gray was.

Gray developed LazyPython and deep_reload, tools that make the interactive prompt easier to use. LazyPython provides automatic quoting, parenning, and shell escapes. With the quoting enhancement a function or method call typed like this:

>>> ,func arg1 arg2 arg3

becomes

>>> func("arg1", "arg2", "arg3")

When used as the first character on your prompt line, that little comma lets Python know to take what follows as a function and its arguments. It adds the parentheses and quotes for you.

Similarly, you can add parentheses without the quotes using "/"

>>> /func arg1 arg2 arg3

becomes

>>> func(arg1, arg2, arg3)

or a simple

>>> /popd

becomes

>>> popd()

Some common functions, like cd, ls, rm, and cp are automatically quoted so you don't even have to type a ",". You can add your own favorite functions to the list. Lazy Python also makes it easy to invoke a system command. Called shell escapes, you perform one by prepending your command with a "!".

Related to LazyPython, the deep_reload module is a recursive drop-in replacement for the reload command. It reloads all the modules a given module imports. This can be useful when you want to reload your entire project at once instead of one module at a time. More lazyness.

Enhancing the Python prompt is nothing new. Janko Hauser wrote an enhanced interactive Python prompt called IPP. Like LazyPython, IPP has shell escapes invoked with "!". IPP maintains a prompt history that you can incrementally search by typing ctrl-r. Just type in the letters of what you are looking for, and ipp will search your command history for them. It has tab completion. Tabbing in the middle of a command brings up a list of possible completions. IPP also adds a help system built on top of Jim Eggleston's docreader program. You invoke it with "?". The help system will search through external documentation as well as the docstrings of imported modules. It is similar to pydoc's help method.

Like IPP but simpler, Robin Friedrich's interactive.py offers tab completion, a simple help system, and convenience functions for common shell escapes. The program is meant to be used as a Python startup file. When Python loads its interactive prompt, it looks for a PYTHONSTARTUP environment variable. It will load and execute any file listed in that variable before dropping you at the prompt.

Gray, following the lazy way, has built upon Robin Friedrich's work. He enhanced Friedrichs interactive.py by adding LazyPython and deep_reload to it and changing Friedrich's help function so it uses pydoc. If you don't have LazyPython or deep_reload, the startup file still works, but it's sweet to have them all working together.

Boosting the power of interactive Python further, Gray has written gracePlot, an interactive interface to the 2D plotting tool Grace. Gray has integrated gracePlot with Numerical Python, and turned this C plotting package into a scientific exploration dream tool. You can make changes to the plot on the fly. You can even plot live data as it arrives.

It all adds up to Python power at the keyboard. I haven't given up on my BASH shell yet, but these tools have made playing at the interactive Python prompt much more fun, even when manipulating graphic applications. With gracePlot's interactive enhancements, Grace is a visualization tool with strong educational and scientific applications. Best of all, these tools have given me more opportunities to make my co-workers say, "wow!"

Stephen Figgins is an editor with the O'Reilly Network and the network's Python DevCenter bureau chief.

Read more Python News columns.

Return to Related Articles from the O'Reilly Network .


Library Navigation Links

oreillynet.com Copyright © 2003 O'Reilly & Associates, Inc.