ANN: Interactive Python Prompt Tools

Nathaniel Gray
Fri, 28 Sep 2001 17:05:21 -0700


                     Announcing:  LazyPython v0.5
                                  deep_reload v0.5
                 Tools for the interactive Python prompt.



LazyPython is an exception hook for Python versions 2.1 and later that brings 
shell escapes, auto-parenthesizing and auto-quoting to the python interactive 

deep_reload is an import hook for Python versions 1.5.2 and above that causes 
the reload() command to act recursively, reloading all of the modules that a 
given module imports.

Together they enhance the convenience of the Python prompt as an 
interactive environment like, for example, Matlab.


Here's an example LazyPython session:
    >>> import LazyPython
    Welcome to Lazy Python.  Type "help LazyPython" for help.
    >>> sys.excepthook = LazyPython.LazyPython()
    >>> # '!' is the shell escape character
    >>> !pwd
    >>> !ls
    CVS                     n8utils.pyc          
        ... snip listing ...
    >>> # '/' is the auto-paren escape character
    >>> import gracePlot
    >>> /reload gracePlot    # Look Ma, no parens!
    --> reload(gracePlot)
    >>> # When LazyPython rewrites your command line it notifies you with
    >>> # the line "--> <new-cmd-line>\n\n"
    >>> # Since reload is a callable, you don't really even have to escape it!
    >>> reload gracePlot     # No '/' or parens
    --> reload(gracePlot)
    >>> # ',' is the auto-quote escape character
    >>> ,some_function arg1 arg2 arg3
    --> some_function("arg1", "arg2", "arg3")    
    >>> # You can also specify a list of auto-quote functions that you don't
    >>> # need to escape. Assuming we have a convenience function 'cp' 
    >>> # that we've specified as an auto-quote function:
    >>> cp somefile someotherfile    # Yes, you could also use a shell escape
    --> cp("somefile", "someotherfile")

Here's how you use deep_reload:
    >>> import __builtin__, deep_reload
    >>> __builtin__.reload = deep_reload.reload
    >>> import Numeric
    >>> reload Numeric      # LazyPython auto-parenning  :-)
    --> reload(Numeric)

    Reloading Numeric
    Reloading numeric_version
    Reloading multiarray
    Reloading umath
        ... snip a bunch of other modules ...
    Reloading copy
    Reloading org
    <module 'Numeric' from '/usr/local/[snip...]/Numeric.pyc'>


The interactive capability of Python is one of its shining strengths.  However,
it seems that the interactive prompt was not intended to be the primary
working environment for Python programmers and it lacks certain convenience 
features, like shell escapes, that would enable this usage.  As packages such
as Matlab demonstrate, though, an interactive prompt can be a very effective
working environment, and thanks to advances such as pydoc, Python is achingly
close to providing an interactive prompt that one can work at for hours at a
time.  These files aim to bring the Python prompt closer to dedicated 
interactive environments such as Matlab in terms of convenience and 

Working interactively is much different than writing a program.  Every
keystroke is precious.  This is why the syntax of shells are so different from
that of most programming languages.  This, plus the utility of shell escapes,
is the motivation for LazyPython.

Also, when working interactively there are many times when one wishes to reload
his entire project at once rather than one module at a time.  This is when
deep_reload comes in useful.

Using these utilities I have successfully and comfortably used Python as an
interactive scientific computing environment.


Both files can be obtained at:


Nathaniel Gray

California Institute of Technology
Computation and Neural Systems