简体   繁体   English

如何从 windows 命令提示符中杀死 python?

[英]how to kill python from windows command prompt?

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当我的 python 脚本运行代码完成后,我希望能够从 windows 命令行中终止 python 脚本,并关闭与 python 脚本有关的所有内容,然后关闭与 windows 相关的所有内容。

the python window remains open after all code has been executed even though I have included exit() at the end of my code python window 在所有代码执行后保持打开状态,即使我在代码末尾包含了exit()

i have tried我努力了

taskkill /IM py.exe

ERROR: The process "py.exe" not found.错误:找不到进程“py.exe”。

taskkill /IM myScript.exe

ERROR: The process "myScript.exe" not found.错误:找不到进程“myScript.exe”。

i cannot find any python processes in task manager我在任务管理器中找不到任何 python 进程

can anyone help?谁能帮忙?

在此处输入图像描述

To kill a task/process either by using the process id or by the image file name.要么通过使用进程ID或图像文件名杀死一个任务/进程

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 :通过在 python ide 中添加代码解决了问题:

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

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.您可以添加可选的 /F 标志,这意味着强制停止。

用您的脚本替换your script.py并在 cmd 上运行此代码。

wmic process where "commandline like '%%your script.py%%'" delete
taskkill /IM python.exe /F

This will force to kill the python.这将强制终止 python。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM