views:

29

answers:

2

Hi,

I'm struggling with setting my scons environment variables for visual studio 2008.
Normally I do following:

%VS90COMNTOOLS%vsvars32.bat

or 

call %VS90COMNTOOLS%vsvars32.bat

And this works in my shell.

I try to do that in python using subprocess

subprocess.call([os.environ['VS90COMNTOOLS']+r"\vsvars32.bat"])

output:

d:\N\workspace>scons test
scons: Reading SConscript files ...
Setting environment for using Microsoft Visual Studio 2008 x86 tools.
KeyError: 'INCLUDE':

above invokes batch process, but environment variables are not inherited from it to my 'master process.

When i use:

subprocess.call([os.environ['VS90COMNTOOLS']+r"\vsvars32.bat"])

I get:

d:\N\workspace>scons test
scons: Reading SConscript files ...
WindowsError: [Error 2] The system cannot find the file specified:
  File "D:\N\workspace\SConstruct", line 17:
    subprocess.Popen(["call ", os.environ['VS90COMNTOOLS']+r"\vsvars32.bat"])
  File "C:\Python26\lib\subprocess.py", line 595:
    errread, errwrite)
  File "C:\Python26\lib\subprocess.py", line 821:
    startupinfo)

How to achieve that?

A: 

Write a batch file that runs vsvars32.bat and then outputs the values in the form VARNAME=value, then have your Python script parse the values and inject them into os.environ.

Ignacio Vazquez-Abrams
I was not considering this way, but it seems that there is no way to do that using python subprocess library (no mystery hidden options;).So i'll accept your answer.
bua
The reason there's no way to do this is because each process has their own environment, and environments can only be handed down to child processes, not passed up to parent processes. So we use an "out-of-band" communication method to pass what the values *should* be to the parent.
Ignacio Vazquez-Abrams
A: 

In addition to the previous answer. An excerpt of my SConstruct:

for key in ['INCLUDE','LIB']:
    if os.environ.has_key(key):
        env.Prepend(ENV = {key.upper():os.environ[key]})

Please take care that variable names in Python are case-sensitive. Ensure that your env['ENV'] dict has no duplicate variable names with different case, otherwise the Windows shell will only see one variant of the variable.

Wolfgang Ulmer