How to call script in interative status

Fernando Perez fperez528 at yahoo.com
Fri Feb 28 14:22:53 EST 2003


Fu Chen wrote:

> then in the command line >>>, how can i run it with args?
> import script
> script.main()
> but where is the args to input?


If you want to try out ipython
(http://www-hep.colorado.edu/~fperez/ipython/), the 'run' command will do
all this (and more) for you.  Here's the docstring for it:

Docstring:
    Run the named file inside IPython as a program.

    Usage:
      @run [-n -i -p [profile options]] file [args]

    Parameters after the filename are passed as command-line arguments to
    the program (put in sys.argv). Then, control returns to IPython's
    prompt.

    This is similar to running at a system prompt:
      $ python file args
    but has the advantage of giving you IPython's tracebacks, and of
    loading all variables into your interactive namespace for further use
    (unless -p is used, see below).

    The file is executed in a namespace initially consisting only of
    __name__=='__main__' and sys.argv constructed as indicated. It thus
    sees its environment as if it were being run as a stand-alone
    program. But after execution, the IPython interactive namespace gets
    updated with all variables defined in the program (except for __name__
    and sys.argv). This allows for very convenient loading of code for
    interactive work, while giving each program a 'clean sheet' to run in.

    Options:

    -n: __name__ is NOT set to '__main__'. This allows running scripts and
    reloading the definitions in them without triggering a call to testing
    routines which are often wrapped in an 'if __name__=="__main__" '
    clause.

    -i: run the file in IPython's namespace instead of an empty one. This
    is useful if you are experimenting with code written in a text editor
    which depends on variables defined interactively.

    -p: run program under the control of the Python profiler module (which
    prints a detailed report of execution times, function calls, etc).

    You can pass other options after -p which affect the behavior of the
    profiler itself. See the docs for @prun for details.

    In this mode, the program's variables do NOT propagate back to the
    IPython interactive namespace (because they remain in the namespace
    where the profiler executes them).

    Internally this triggers a call to @prun, see its documentation for
    details on the options available specifically for profiling.






More information about the Python-list mailing list