stop script w/o exiting interpreter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Isaac

    #1

    stop script w/o exiting interpreter

    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


  • Paul Rubin

    #2
    Re: stop script w/o exiting interpreter

    "Alan Isaac" <aisaac@america n.eduwrites:
    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.

    Comment

    • Miki

      #3
      Re: stop script w/o exiting interpreter

      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
      If it won't be simple, it simply won't be. [Hire me, source code]


      Comment

      • Bruno Desthuilliers

        #4
        Re: stop script w/o exiting interpreter

        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(s ys.argv))

        Comment

        • Colin J. Williams

          #5
          Re: stop script w/o exiting interpreter

          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.


          Colin W.

          Comment

          • Alan Isaac

            #6
            Re: stop script w/o exiting interpreter

            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


            Comment

            • Gabriel Genellina

              #7
              Re: stop script w/o exiting interpreter

              "Alan Isaac" <aisaac@america n.eduescribió en el mensaje
              news:wWQuh.1259 $SE6.1215@trndd c03...
              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


              Comment

              Working...