+ Reply to Thread
Results 1 to 9 of 9

Thread: How to stop a command or prompt in Python?

  1. #1
    Dave Couture
    Join Date
    Sep 2010
    Posts
    128
    Points
    2
    Answers Provided
    1


    0

    Default How to stop a command or prompt in Python?

    I'm using ArcGIS 10 and I'm practicing a few scripts with Python. What command should I use, if I want to exit a commnad, or a function? I'm always stuck with 3 dots (secondary prompt). How do I stop it?

    I've tried:

    sys.exit()
    sys.exit(0)
    quit()

  2. #2
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,261
    Points
    414
    Answers Provided
    38


    0

    Default Re: How to stop a command or prompt in Python?

    I usually use sys.exit() to stop a script from running if some error condition is met. So for example, if a user put in a wrong input variable...

    Code:
    import sys
    var =gp.GetParameterAsText(0)
    if var not in ("cat","dog","mouse"):
       print "ERROR: You entered the wrong animal! Exiting script..."; sys.exit()
    else:
       print var + " is a valid entry. Good job!"
    You might be intersted in the pass or break statements too...

    pass is used to basically 'do nothing', so something like:

    Code:
    if animal == "dog":
       pass #aka  don't do anything
    else:
       print "not a dog"
    break is used to break out of a while or for loop, so something like:

    Code:
    for x in (1,2,3,4,5):    
        if x > 4:
           break #aka stop looping
        else:
           print x

  3. #3
    Dave Couture
    Join Date
    Sep 2010
    Posts
    128
    Points
    2
    Answers Provided
    1


    0

    Default Re: How to stop a command or prompt in Python?

    Thanks for the reply and the useful info. However, it's not really what I need. I'm trying to stop everthing and go back to the >>>. But I can't, I'm always stuck with the three dots (...).

    For example, if I type the following command (import sys) and I decide to change my mind and exit out of that command, how would I do it? The sys.exit() doesn't work.

    Here's what I get:

    >>> import sys
    ... sys.exit()
    ...

    I'm always stuck with the 3 dots...why?

  4. #4
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,261
    Points
    414
    Answers Provided
    38


    0

    Default Re: How to stop a command or prompt in Python?

    Not sure what you mean exactly...

    And you are hitting the enter button after typing 'import sys', right?

    Are you entering these commands at the prompt in:
    -python.exe (the DOS-looking window)
    -pythonwin.exe (the freebee python IDE)
    -the python window in ArcMap
    -something else?

  5. #5
    Dave Couture
    Join Date
    Sep 2010
    Posts
    128
    Points
    2
    Answers Provided
    1


    0

    Default Re: How to stop a command or prompt in Python?

    I'm doing it in Python in ArcMap and yes, I do hit Enter at the end of the line. I have experience with VBA and C++, but not with Python. The compiler is really different; it's like scripting live, instead of hitting a RUN button, when you are done scripting....it's more similar to DOS, then anything else. It takes some getting used to!

    I'm just looking for the magic command, to get out of any command that I'm in.
    Last edited by davecouture; 03-31-2011 at 11:15 AM.

  6. #6
    Logan Pugh
    Join Date
    May 2010
    Posts
    210
    Points
    27
    Answers Provided
    1


    0

    Default Re: How to stop a command or prompt in Python?

    The magic command you're probably looking for is Ctrl-C (Ctrl-Break on some machines), but disappointingly, it does not work at the ArcGIS Python prompt, and even in a standalone console window, if the arcgisscripting (and presumably arcpy) module is loaded then the expected Ctrl-C behavior (throwing a KeyboardInterrupt exception) is broken. I detailed this problem and a workaround here, but again this would most likely not work within the ArcGIS Python prompt presumably because the keyboard accelerators used by the main application are probably lower level/higher priority so they don't reach the Python interpreter.

    What specific commands are locking you up? An import statement as in your example should not cause multi-line entry nor long execution times. If statements, loops, try/except/finally constructs and function definitions can though, and the termination/exit command for those are various, such as pass, break, or return. When entering a multi-line construct if you just hit enter or escape does it not return you to the main command prompt?

    For anything more significant than poking around arcpy or calling single GP tools you should probably be writing scripts in a real IDE instead of using the ArcGIS Python prompt anyways.

  7. #7
    Dave Couture
    Join Date
    Sep 2010
    Posts
    128
    Points
    2
    Answers Provided
    1


    0

    Default Re: How to stop a command or prompt in Python?

    Thanks for all the info, I'll try the IDE.

  8. #8
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,261
    Points
    414
    Answers Provided
    38


    0

    Default Re: How to stop a command or prompt in Python?

    The ArcMap Python window "claims" that Esc will do it, but... not so sure.

  9. #9
    Sam Roberson
    Join Date
    Jun 2013
    Posts
    1
    Points
    0
    Answers Provided
    0


    0

    Default Re: How to stop a command or prompt in Python?

    I can vouch for the effectiveness of the following process:

    1. Click on the 'Results' window,

    2. Click on the 'Current Session' text,

    3. Right-click on the uppermost process (i.e. the one that is currently running) and select 'Cancel'.

    This should interupt all processes, unless there is a statement that catches errors, in which case you may have to repeat the process a few times.

  10. 06-10-2013 05:31 PM


+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts