How to stop a command or prompt in Python?

60025
10
03-31-2011 09:25 AM
DaveCouture
New Contributor III
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()
Tags (2)
0 Kudos
10 Replies
ChrisSnyder
Regular Contributor III
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...

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:

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:

for x in (1,2,3,4,5):    
    if x > 4:
       break #aka stop looping
    else:
       print x
0 Kudos
DaveCouture
New Contributor III
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?
0 Kudos
ChrisSnyder
Regular Contributor III
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?
0 Kudos
DaveCouture
New Contributor III
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.
0 Kudos
LoganPugh
Occasional Contributor III
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.
0 Kudos
DaveCouture
New Contributor III
Thanks for all the info, I'll try the IDE.
0 Kudos
ChrisSnyder
Regular Contributor III
The ArcMap Python window "claims" that Esc will do it, but... not so sure.
SamRoberson
New Contributor
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.
0 Kudos
JohnChurchill
New Contributor III

This did not work for me. I had a loop running a selection and writing values to a text file and cancel would only effectively cancel the current selection. I tried cancel about 10 times before calling it quits. One thing that will always work is "End Process" in the task manager but that is probably not recommended.

0 Kudos