Python Stop Execution Of Script

To stop a python script just press Ctrl + C. Inside a script with exit, you can do it. You can do it in an interactive script with just exit. You can use pkill -f name-of-the-python-script. In this case, break will stop all script execution, even if called within a function in the script. It's a great way to ensure the entire script halts wherever it may be (excluding in a loop or switch statement). As you can see, there are many ways to either stop or redirect code execution in a PowerShell script. In general, typing Control+C cannot be counted on to interrupt a running Python program. Depending on what is happening in your loop: 1) Canopy's Run menu Interrupt kernel (for most simple programs, this will work).

P: n/a
Hi All,
(sorry for my bad english)
I wrote a __tiny__ and __stupid__ recursive script directly into
pythonwin
interactive window with a time.sleep(1) and a print before each
recursion...
I should have taken a closer look at the ending condition (never
satisfied!),
anyway I was quite confident that a control-C would have stopped the
intepreter as it is (incidentally?) when this break sequence is entered
during a screen-i/o of the python interpreter in a CMD prompt...
Instead I discovered my pythonwin session no more responding even
though the output shows that it was still working correctly... and my
other
open files in pythonwin still needing to be saved - my salvation was
that
while I was searching a solution with google, after 984 nested call
~ more than a quarter later, the recursion stack was full and
an exception was raised! ;-P).
So my question is: is there a keystroke combination to stop the
interpreter
in pythonwin interactive window? Or even better
Is there a 'pythonwin interactive window' keystrokes list?
(btw: I remember an old post explaining the keystroke to
reset interactive window memory without being forced to
close and open pythonwin - very usefull but I could not find it
anymore...)
TIA!
bye,
PiErre
Active6 years, 10 months ago

(5 replies) We let users execute scripts in our app using the exec command. We want to provide a stop button to stop script execution but how is the best way to do that? Can a background worker be used for that? Thanks for help! Jun 10, 2013  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. You need to explicitly tell the bat file to not run successive python scripts if the current script fails and that is what the fail log is for. Now if python scripts are not dependent on each other for working properly, then your current setup could work.

Is it possible to break the execution of a Python script called with the execfile function without using an if/else statement? I've tried exit(), but it doesn't allow main.py to finish.

JcMaco
JcMacoJcMaco
6751 gold badge13 silver badges30 bronze badges

3 Answers

main can wrap the execfile into a try/except block: sys.exit raises a SystemExit exception which main can catch in the except clause in order to continue its execution normally, if desired. I.e., in main.py:

Stop

and whatever.py can use sys.exit(0) or whatever to terminate its own execution only. Any other exception will work as well as long as it's agreed between the source to be execfiled and the source doing the execfile call -- but SystemExit is particularly suitable as its meaning is pretty clear!

Alex Martelli

How To Stop Python Execution

Alex Martelli
665k136 gold badges1067 silver badges1297 bronze badges

I find this aspect of Python (the __name__'__main__', etc.) irritating.

Matthew Flaschen

Python Stop Program

Matthew Flaschen
230k39 gold badges454 silver badges511 bronze badges

What's wrong with plain old exception handling?

scriptexit.py

main.py

script.py

Python Stop Execution Of Script Video

S.LottS.Lott

Python Stop Execution Of Script Free

330k71 gold badges457 silver badges732 bronze badges

Python Stop Execution Of Script Format

Not the answer you're looking for? Browse other questions tagged pythonflow-controlexecfile or ask your own question.