Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Quit IDLE after Script running

Is it possible to somehow quit Python IDLE under Windows from within my python code/script at the end after running?

I tried something like:

import sys
sys.exit(0) # or exit(1)

didn't work. Probably only quits the current "process" of the running python script. Thanks a lot

like image 592
tim Avatar asked Jul 31 '26 12:07

tim


2 Answers

If you can run IDLE from the command line (or edit your shortcut), I found this in the IDLE help.

If IDLE is started with the -n command line switch it will run in a
single process and will not create the subprocess which runs the RPC
Python execution server.

Which seems to imply that the script is running inside IDLE. I did a quick test script, and calling exit() brought up a box asking to kill the program. Clicked yes, and the script and IDLE both quit. Hope that can help.

like image 120
CDspace Avatar answered Aug 02 '26 03:08

CDspace


This will do exactly what you want. No prompt.

import os
os.system("taskkill /f /im pythonw.exe")
like image 25
Charles Clayton Avatar answered Aug 02 '26 02:08

Charles Clayton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!