This content has been marked as final. Show 9 replies
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
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
I'm always stuck with the 3 dots...why?
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
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.
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.
Thanks for all the info, I'll try the IDE.
The ArcMap Python window "claims" that Esc will do it, but... not so sure.
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.
It almost sounds to me like you have a corrupt python install.
In python.exe, IDE, or the python window in ArcMap, I can not repeat. The only time I can get the ... is if I do a multiline entry ( IE, if this == "that":) or nested code.
an import statement should never be "seen" as a multiline entry or nested code.
Curious what happens if you type this:
Do you get:
or do you get:
If you get the latter, I would try a remove/re-install of your python.