简体   繁体   English

批处理脚本完成后如何关闭命令窗口

[英]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如果您有并喜欢它) ,我使用了您提供的acivateforward_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.

相关问题 关闭在 web 浏览器上运行的脚本后如何关闭命令行 window? - How to close command line window after closing script running on web browser? 批处理运行python脚本..最后如何关闭cmd窗口? - Batch runs python script.. how to close cmd window at end? 关闭 Python Selenium 的命令行 window 使用 ZC89686A387D2BABC55BBatch3 运行后 - Close Python Selenium 's command line window after it runs using Window Batch 如何在运行后强制挂起或暂停python脚本,以便在完成后不强制关闭? - How to hard suspend or pause a python script after it runs so it doesn’t force close upon completion? 打开新的cmd窗口后,如何从python3批处理文件在终端中推送命令? - How to push in a command in terminal from a python3 batch file after having opened a new cmd window? 执行关闭窗口命令后,Pyqt终端挂起 - Pyqt Terminal hangs after excuting close window command 选择脚本的输出文件夹后无法关闭Tkinter窗口 - Can't close Tkinter window after selecting output folder for script 如何在我的 python 脚本中运行批处理命令? - How to run batch command in my python script? 如何破解IntelliJ Idea命令行启动器脚本以在独立窗口中打开文件并等待关闭? - How can I hack the IntelliJ Idea Command-line Launcher script to open a file in an independent window and wait for close? 如何在点击按钮后打开另一个window并关闭当前的window? - How to open another window and close the current window after clicking button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM