如何在.bat批处理文件中启动Python虚拟环境? [英] How to start Python virtual environment in .bat batch file?

查看:91
本文介绍了如何在.bat批处理文件中启动Python虚拟环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次在Windows 10上启动Python Flask应用程序时,我都需要设置一长串临时环境变量.现在,我想创建一个批处理文件以双击运行所有设置.如果我将它们复制并粘贴到cmd提示符下,则以下行可以很好地运行,但无法在批处理文件中运行它们.

I need to set up a long list of temporary environment variables every time when I start a Python Flask app on Windows 10. Now I would like to create a batch file to run with all settings in one double click. The following lines run well if I copy them and paste to cmd prompt, but I couldn't run them in the batch file.

批处理文件的执行总是在批处理文件的第二行 venv \ scripts \ activate 处触发并退出,如果我在cmd上逐行复制并粘贴,则根本没有问题

The execution of batch file always gets tripped and exited at the 2nd line venv\scripts\activate in the batch file, which has no issue at all if I copy and paste line by line at cmd.

cd C:\py\project
venv\scripts\activate

set env1=val1
set env2=val2
set FLASK_APP=some.py

flask run

推荐答案

.bat文件的许多(非常多)怪癖之一就是,如果启动另一个.bat文件,它不知道返回到哪里..
您需要明确地调用它:

One of the many (far too many) quirks of .bat files is that if you launch another .bat file, it doesn't know where to return to.
You need to explicitly call it:

call venv\scripts\activate

这篇关于如何在.bat批处理文件中启动Python虚拟环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆