在运行当前进程虽然蟒蛇一个bat文件 [英] running a bat file though python in current process

查看:200
本文介绍了在运行当前进程虽然蟒蛇一个bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一个python脚本来构建一个大系统。我首先需要建立的Visual Studio环境。有问题我决定看看我是否可以只设立并启动Visual Studio中。我先设置一些环境变量,然后调用 C:\\ Program Files文件(x86)的\\微软的Visual Studio 11.0 \\ VC \\ vcvarsall.bat 64

I am attempting to build a large system through a python script. I first need to set up the environment for Visual Studio. Having problems I decided to see if I could just set up and launch Visual Studio. I first set several environment variables and then call C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat x64.

在此结束我称之为 devenv的了/ useenv 。如果我的命令做这些提示的一切工作正常,我可以做什么,我需要在VS.做我的Python code这样做是这样的:

Once this finishes I call devenv /useenv. If I do these from the command prompt everything works fine and I can do what I need to do in VS. My python code for doing this is:

import os
vcdir=os.environ['ProgramFiles(x86)']
arch = 'x64'
command  = 'CALL "' +vcdir+'\\Microsoft Visual Studio 11.0\\VC\\vcvarsall.bat" '+arch
os.system(command)
command = "CALL devenv /useenv"
os.system(command)

如果我运行此,BAT文件运行时它会尝试 devenv的命令我得到的,它无法识别。它看起来像蝙蝠文件在不同的子进程比脚本运行中的一个运行。我真的需要在我目前这个过程中运行。我的最终目标是做python脚本里面的整个构建会有很多调用 devenv的做构建的主要部分。

If I run this, the bat file will run and when it tries the devenv command I get that it is not recognized. It looks like to bat file runs in a different subprocess than the one that the script is running in. I really need to get this running in my current process. My eventual goal is to do the entire build inside the python script and there will be many calls to devenv to do a major portion of the build.

感谢您。

推荐答案

你正在尝试做是行不通的。 vcvarsall.bat 设置环境变量,其中只有一个特定的进程内运行。有没有办法让一个Python进程运行相同的进程空间内的的CMD.exe 的过程。

What you are trying to do won't work. vcvarsall.bat sets environment variables, which only work inside a particular process. And there is no way for a Python process to run a CMD.exe process within the same process space.

我有几个解决方案,为你。

I have several solutions for you.

一是运行 vcvarsall.bat ,然后计算出所有它设置环境变量并使Python设置它们。您可以使用命令集> file.txt的来保存所有来自CMD壳中的所有环境变量,然后编写Python code解析这个文件,并设置环境变量。也许太多的工作。

One is to run vcvarsall.bat, then figure out all the environment variables it sets and make Python set them for you. You can use the command set > file.txt to save all the environment variables from a CMD shell, then write Python code to parse this file and set the environment variables. Probably too much work.

二是使运行 vcvarsall.bat ,然后从该批处理文件中触发了一个新的Python间preTER一个批处理文件。在新的Python间preTER将是一个新的进程,但它会在的CMD.exe 进程下一个子进程,它将继承所有环境变量

The other is to make a batch file that runs vcvarsall.bat, and then fires up a new Python interpreter from inside that batch file. The new Python interpreter will be in a new process, but it will be a child process under the CMD.exe process, and it will inherit all the environment variables.

或者,嗯。也许最好的事情就是写运行 vcvarsall.bat ,然后运行 devenv的命令的批处理文件。是的,这可能是最简单的,而简单的还是不错的。

Or, hmm. Maybe the best thing is just to write a batch file that runs vcvarsall.bat and then runs the devenv command. Yeah, that's probably simplest, and simple is good.

要注意的重要一点是,在Windows的批处理文件的语言(之前DOS批处理文件吧),当你执行另一个批处理文件,由其他批处理文件设置变量在相同的环境设置。在* NIX你需要使用一个特殊的命令在同一环境中运行一个shell脚本,但在批量这就是它是如何工作的。但你永远不会得到变量坚持一个进程终止后。

The important thing to note is that in the Windows batch file language (and DOS batch file before it), when you execute another batch file, variables set by the other batch file are set in the same environment. In *NIX you need to use a special command source to run a shell script in the same environment, but in batch that's just how it works. But you will never get variables to persist after a process has terminated.

这篇关于在运行当前进程虽然蟒蛇一个bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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