如何运行vcvarsall.bat/vcvars64.bat作为Python的子过程
我正在尝试运行VCVARSALL.bat BAT脚本以初始化所有环境变量,以便可以运行后续命令,但是它似乎无法按照我喜欢的方式来工作。这是我的代码的片段:
try:
path = r'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat'
subprocess.Popen([vcvars, 'x64'], shell=True)
except FileNotFoundError:
print("Visual Studio is not installed on this system")
我还将在此之后打印出路径变量,但是在此处运行此功能后,它似乎并没有粘贴。这甚至可能吗?
I am trying to run the vcvarsall.bat bat script to initialize all the environment variables so that I can run follow up commands, but it does not seem to be working the way I'd like it to. Here's a snippet of my code:
try:
path = r'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat'
subprocess.Popen([vcvars, 'x64'], shell=True)
except FileNotFoundError:
print("Visual Studio is not installed on this system")
I also print the PATH variable after, but it does not seem to stick after running this function here. Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您执行
vcvarsall.bat
时,您正在创建一个开发环境,但是一旦子进程终止,创建的环境就会被破坏。子进程所做的环境修改绝不会返回到父进程的环境中 - 这是正常的,是故意的,如果不是这种情况,在大多数操作系统中很多事情都会立即中断。您应该在运行 Python 和您的程序之前调用此批处理,以便所有必需的变量都已在环境中。
You're creating a development environment when you execute
vcvarsall.bat
, but once the child process is terminated, the created environment is destroyed. In no way environment modifications done by a child process would be brought back in parent's environment - that's normal, on purpose, and a LOT of things would break instantly in most operating systems if it wasn't the case.You should call this batch BEFORE running Python and your program, so all required variables will be already in environment.