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.
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)
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.
Thank you.
What you are trying to do won’t work.
vcvarsall.batsets environment variables, which only work inside a particular process. And there is no way for a Python process to run aCMD.exeprocess within the same process space.I have several solutions for you.
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 commandset > file.txtto 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.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 theCMD.exeprocess, and it will inherit all the environment variables.Or, hmm. Maybe the best thing is just to write a batch file that runs
vcvarsall.batand then runs thedevenvcommand. Yeah, that’s probably simplest, and simple is good.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
sourceto 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.