how to kill python from windows command prompt?

Viewed 18959

When my python script is done running code i want to be able to kill the python script from windows command line only and close everything that has to do with python

the python window remains open after all code has been executed even though I have included exit() at the end of my code

i have tried

taskkill /IM py.exe

ERROR: The process "py.exe" not found.

taskkill /IM myScript.exe

ERROR: The process "myScript.exe" not found.

i cannot find any python processes in task manager

can anyone help?

enter image description here

6 Answers

To kill a task/process either by using the process id or by the image file name.

taskkill /IM executablename

OR

First detects if a process is running, then kills

tasklist | find /i "executablename.exe" && taskkill /im "executablename.exe" /F || echo process "executablename.exe" not running

Just make batch file & run it.

@echo off
FOR /F "tokens=2 delims= " %%P IN ('tasklist /FO Table /M /NH ^| Find /i "Py"') DO (TASKKILL /PID %%P /F)

Solved problem by adding code in python ide :

import os
os.system("taskkill /im py.exe")
taskkill /IM python.exe /F

This will force to kill the python.

Even this can be tried:

@echo off
tasklist /fi "IMAGENAME eq name.exe" && (taskill /Im "name.exe")

You can add optional /F flag that means forcefully stopping.

To replace your script.py with your script and run this code on cmd.

wmic process where "commandline like '%%your script.py%%'" delete
Related