[英]How to close command window after batch script completion
I have created a batch script to run my python script which needs to be run in a virtual environment.我创建了一个批处理脚本来运行需要在虚拟环境中运行的 python 脚本。 I have added 'exit' to the end of my batch scrip but the command window stays open.我在批处理脚本的末尾添加了“退出”,但命令窗口保持打开状态。 My script is below:我的脚本如下:
@echo off
cmd /k "C:\Users\user\Documents\venv\Scripts\activate & C:\Users\user\Document\forward_test.py"
exit
I think it has something to do with how I am activating the environment but I am not very experienced with batch script.我认为这与我如何激活环境有关,但我对批处理脚本的经验不是很丰富。 Does anyone know what's going on here?有谁知道这里发生了什么?
From the output of cmd /?
从cmd /?
: :
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
Because of the /k
in your command, the new window is opening, running your environment and python scripts, and then staying open.由于命令中的/k
,新窗口正在打开,运行您的环境和 python 脚本,然后保持打开状态。 If you change the /k
to /c
, the window will close once the python script has terminated.如果将/k
更改为/c
,则在 python 脚本终止后窗口将关闭。
You are already running in cmd.exe, because you've invoked a batch file.您已经在 cmd.exe 中运行,因为您调用了一个批处理文件。 Therefore all you need is to activate your environment and run your python script:因此,您只需要激活您的环境并运行您的 python 脚本:
@Call "%UserProfile%\Documents\venv\Scripts\activate.bat"
@"P:\ath\To\python.exe" "%UserProfile%\Document\forward_test.py"
Please note, that you will need to insert the correct location for your python.exe
, (or py.exe
if you have and prefer it) , I used your provided paths for acivate
and forward_test
, please make sure that you use the correct ones, (as it looks like you may have submitted code with one or more typos) .请注意,您需要为您的python.exe
插入正确的位置(或py.exe
如果您有并喜欢它) ,我使用了您提供的acivate
和forward_test
路径,请确保您使用正确的路径, (看起来您提交的代码可能有一个或多个错别字) 。
Additionally, if the python script runs something in the background, (doesn't output to the console and takes a while) , you may wish to change your second line to:此外,如果 python 脚本在后台运行某些内容(不输出到控制台并需要一段时间) ,您可能希望将第二行更改为:
@Start "" "P:\ath\To\python.exe" "%UserProfile%\Document\forward_test.py"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.