Exit Python Execution

How does one stop execution of a python script without the system trying to exit the Rhino process? It seems all the standard means for doing this (quit(), sys.exit(), etc.) all raise the SystemExit exception which tells Rhino to quit.

edit: for instance, is there a way to emulate sending keystrokes (like C-c) to the interpreter in rhino?

Hi Bert,

why not use return None to get out of the main loop instead of quit() ? Or did i misunderstood your question and are trying to cancel eg. a tight loop using a key press ?

c

@clement That was my original thought, it’s just that the location where i need to conditionally exit is not inside a function. My code is structured like this:

imports

setup code (check for dongle, exit if not found, otherwise exec some code from the dongle)

main(){}

if name==“main”:
main()

i can certainly move the setup code inside main, it just interested me that Rhino was exiting when I used exit(). I thought exit() was just supposed to quit the interpreter; i guess i don’t understand the intricacies about how the interpreter runs when hosted in another application.

edit: I suppose the easier fix would be to add a second conditional check in the final if clause to avoid running main() altogether.

Always use functions. Just create a Setup function and call that from main.

Yes, and/or maybe put the setup code in a function too if possible.

c.

How can i stop my python script if a condition is False…

the exit sub of VB?

assuming you have defined your code in a function, return brings you one level up

def main():
	if not 200 < 42:
		print('exiting')
		return
	print('continuing')
		
if __name__=='__main__':
	main()

1 Like

Ok… Thank You…
I Searched an exit or something like this…