stop script w/o exiting interpreter

Discussion in 'Python' started by Alan Isaac, Jan 25, 2007.

  1. Alan Isaac

    Alan Isaac Guest

    I'm fairly new to Python and I've lately been running a script at
    the interpreter while working on it. Sometimes I only want to
    run the first quarter or half etc. What is the "good" way to do this?

    Possible ugly hacks include:

    - stick an undefined name at the desired stop point
    - comment out the last half

    I do not like these and assume that I have overlooked the obvious.

    Thanks,
    Alan Isaac
    Alan Isaac, Jan 25, 2007
    #1
    1. Advertising

  2. Alan Isaac

    Paul Rubin Guest

    "Alan Isaac" <> writes:
    > I'm fairly new to Python and I've lately been running a script at
    > the interpreter while working on it. Sometimes I only want to
    > run the first quarter or half etc. What is the "good" way to do this?


    If it's single threaded then just call sys.exit(). If you don't know
    what single threaded means, your program is definitely single threaded.
    Paul Rubin, Jan 25, 2007
    #2
    1. Advertising

  3. Alan Isaac

    Miki Guest

    Hello Alan,

    > I'm fairly new to Python and I've lately been running a script at
    > the interpreter while working on it. Sometimes I only want to
    > run the first quarter or half etc. What is the "good" way to do this?

    If you want to exit from the program then "raise SystemExit" is what
    you want.
    If you want to enter the debugger, you can do:

    from pdb import set_trace

    ....
    set_trace() # Stop and execute debugger here.
    ....

    HTH,
    --
    Miki
    http://pythonwise.blogspot.com
    Miki, Jan 26, 2007
    #3
  4. Alan Isaac a écrit :
    > I'm fairly new to Python and I've lately been running a script at
    > the interpreter while working on it. Sometimes I only want to
    > run the first quarter or half etc. What is the "good" way to do this?


    If the point is to debug your script, then import pdb; pdb.set_trace()

    > Possible ugly hacks include:
    >
    > - stick an undefined name at the desired stop point
    > - comment out the last half
    >
    > I do not like these and assume that I have overlooked the obvious.


    If you have much of your code in functions/classes etc, and the bare
    minimum[1] at the top level, then you can launch a Python shell, import
    your module, and test functions as you wish...

    [1]:

    import XXX
    import YYY

    # lots of functions/classes etc

    def main(argv):
    # what would have been at the top level

    if __name__ == __main__:
    import sys
    sys.exit(main(sys.argv))
    Bruno Desthuilliers, Jan 26, 2007
    #4
  5. Alan Isaac wrote:
    > I'm fairly new to Python and I've lately been running a script at
    > the interpreter while working on it. Sometimes I only want to
    > run the first quarter or half etc. What is the "good" way to do this?
    >
    > Possible ugly hacks include:
    >
    > - stick an undefined name at the desired stop point
    > - comment out the last half
    >
    > I do not like these and assume that I have overlooked the obvious.
    >
    > Thanks,
    > Alan Isaac
    >
    >

    Alan,

    If you are using Windows, you might consider PyScripter.
    http://mmm-experts.com/Products.aspx?ProductId=4

    Colin W.
    Colin J. Williams, Jan 26, 2007
    #5
  6. Alan Isaac

    Alan Isaac Guest

    Please note that this post has subject
    "stop script w/o exiting interpreter".

    The object is to work at the *interactive* interpreter,
    without leaving it.

    Here is an example goal:
    start a Python shell,
    execfile a script,
    exit the script at line 25,
    and return to the Python shell.

    E.g., some languages include a ``stop`` statement that you can put on line
    25.
    Ideally, I would like the equivalent of this.

    Solutions suggested in this thread included:
    - raise SystemExit
    but this will exit the interpreter
    - sys.exit()
    but this will exit the interpreter
    - use pdb's set_trace()
    but I think that answers a different question.
    (However it does work to raise BdbQuit, but I'd like something less
    messy.)
    - wrap all code in functions and test the functions
    but this does not apply to my current use case
    - use PyScripter
    but this is overkill for my very simple goal

    Note that I can just put the undefined name ``stop`` on any line
    I want, and the script will stop execucting at that line and will
    return to the interactive interpreter, as I wish. It is just that it
    returns with an error message, and I'd like to avoid that.

    Thanks,
    Alan Isaac
    Alan Isaac, Jan 27, 2007
    #6
  7. "Alan Isaac" <> escribió en el mensaje
    news:wWQuh.1259$SE6.1215@trnddc03...

    > Please note that this post has subject
    > "stop script w/o exiting interpreter".
    > Note that I can just put the undefined name ``stop`` on any line
    > I want, and the script will stop execucting at that line and will
    > return to the interactive interpreter, as I wish. It is just that it
    > returns with an error message, and I'd like to avoid that.


    If this is just for playing inside the interpreter, just ignore the
    exception.
    Or comment out all lines from 25 to end of script, some editors (including
    IDLE) have support for that.
    Or use a giant """
    string
    """ if you can.
    If you invoke your script with `python -i your_script.py` Python will show
    the interpreter prompt when your script finishes (either normally or raising
    an exception).

    --
    Gabriel Genellina
    Gabriel Genellina, Jan 28, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Plunket
    Replies:
    6
    Views:
    809
    Peter Hansen
    Jul 15, 2003
  2. John (Z R) L
    Replies:
    6
    Views:
    1,632
    Hertha Steck
    Nov 21, 2006
  3. Replies:
    3
    Views:
    743
    Ziga Seilnacht
    Jan 3, 2007
  4. Peter Juuls
    Replies:
    8
    Views:
    185
    Ian Wilson
    Feb 16, 2006
  5. Mirco Wahab
    Replies:
    1
    Views:
    89
    nikolas pontikos
    Apr 1, 2007
Loading...

Share This Page