如何设置vsvars32环境变量?

发布于 2024-09-26 22:09:34 字数 1088 浏览 7 评论 0原文

我正在努力为 Visual Studio 2008 设置 scons 环境变量。
通常我会执行以下操作:

%VS90COMNTOOLS%vsvars32.bat

or 

call %VS90COMNTOOLS%vsvars32.bat

这在我的 shell 中有效。

我尝试使用子进程

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

输出在 python 中执行此操作:

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

上面调用批处理,但环境变量不会从它继承到我的“主进程”。

当我使用:

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

我得到:

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)

如何实现这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾北清歌寒 2024-10-03 22:09:34

编写一个运行 vsvars32.bat 的批处理文件,然后以 VARNAME=value 形式输出值,然后让 Python 脚本解析这些值并将它们注入 os.environ

这是在 python 自己的 distutils 模块中完成的,在此处查看源代码

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.

This is done in python's own distutils module, see the source here.

岁吢 2024-10-03 22:09:34

除了之前的回答。我的 SConstruct 摘录:

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

请注意 Python 中的变量名称区分大小写。确保您的 env['ENV'] 字典没有大小写不同的重复变量名称,否则 Windows shell 只会看到该变量的一种变体。

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.

〃安静 2024-10-03 22:09:34

对已接受答案的简短代码(Python 3)补充:

def vs_env_dict():
  vsvar32 = '{vscomntools}vsvars32.bat'.format(vscomntools=os.environ['VS140COMNTOOLS'])
  cmd = [vsvar32, '&&', 'set']
  popen = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  stdout, stderr = popen.communicate()
  if popen.wait() != 0:
    raise ValueError(stderr.decode("mbcs")) 
  output = stdout.decode("mbcs").split("\r\n")
  return dict((e[0].upper(), e[1]) for e in [p.rstrip().split("=", 1) for p in output] if len(e) == 2)
os.environ.update(vs_env_dict())

它对我有用!

A short code (Python 3) addition to the accepted answer:

def vs_env_dict():
  vsvar32 = '{vscomntools}vsvars32.bat'.format(vscomntools=os.environ['VS140COMNTOOLS'])
  cmd = [vsvar32, '&&', 'set']
  popen = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  stdout, stderr = popen.communicate()
  if popen.wait() != 0:
    raise ValueError(stderr.decode("mbcs")) 
  output = stdout.decode("mbcs").split("\r\n")
  return dict((e[0].upper(), e[1]) for e in [p.rstrip().split("=", 1) for p in output] if len(e) == 2)
os.environ.update(vs_env_dict())

It works for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文