Reputation: 10176
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
Upvotes: 3
Views: 6938
Reputation: 11
To quit Python IDLE under Windows from within a python code/script without a further prompt, try:
parent_pid = os.getppid() # Get parent's process id
parent_process = None
for proc in psutil.process_iter(attrs=['pid']): # Check all processes
if proc.pid == parent_pid: # Parent's Process class
parent_process = proc
break
if parent_process is not None:
parent_process.kill() # Kill the parent's process
This works with IDLE, PyCharm and even the command line in Windows.
Upvotes: 1
Reputation: 17946
This will do exactly what you want. No prompt.
import os
os.system("taskkill /f /im pythonw.exe")
Upvotes: 4
Reputation: 2689
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.
Upvotes: 4