popen / batchfile / environment variables

logistix logistix at zworg.com
Tue Nov 19 21:40:44 EST 2002


"Achim Domma" <achim.domma at syynx.de> wrote in message news:<are0ca$bm$07$1 at news.t-online.com>...
> Hello,
> 
> I want to write a python script with automaticaly builds a VC.Net solution.
> To make VS.Net usable from commandline, I first have to execute a batchfile
> which sets the required environment variables. In a Dos-Box I would do
> something like this:
> 
> path_to\vcvars32.bat
> devenv solution_file.sln /rebuild "Release"
> 
> If I use popen or system to do the same, every call runs in his own
> environment, so the changes made by the .bat file do not change the settings
> for the second call.
> 
> Any idea how to do that in python?
> 
> regards,
> Achim

Use popen2.

Alternately, set the vars in autoexec.bat (not the best choice on a
new machine)  or set them permenantly by right-clicking on My
Computer, Selecting properties... Advanced... Environment Variables
(or use setx).

>>> from popen2 import popen2
>>> output, input = popen2("cmd.exe")
>>> input.write("set foo=bar\n")
>>> input.write("set\n")
>>> input.write("exit\n")
>>> while 1:
... 	text = output.readline()
... 	if not text: break
... 	print text ,
... 	
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Python22\lib\site-packages>set foo=bar

C:\Python22\lib\site-packages>set
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\grant\Application Data
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=XP
ComSpec=C:\WINDOWS\system32\cmd.exe
foo=bar
HOMEDRIVE=C:
HOMEPATH=\Documents and Settings\grant
INCLUDE=C:\Program Files\Microsoft Visual Studio
.NET\FrameworkSDK\include\
LIB=C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Lib\
LOGONSERVER=\\XP
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program
Files\Support Tools\;C:\Program Files\Resource Kit\;C:\Program
Files\Microsoft SQL Server\80\Tools\Binn\;c:\program files\cvs for
nt\;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 8 Stepping 3, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0803
ProgramFiles=C:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\DOCUME~1\grant\LOCALS~1\Temp
TMP=C:\DOCUME~1\grant\LOCALS~1\Temp
USERDOMAIN=XP
USERNAME=grant
USERPROFILE=C:\Documents and Settings\grant
VSCOMNTOOLS="C:\Program Files\Microsoft Visual Studio
.NET\Common7\Tools\"
windir=C:\WINDOWS

C:\Python22\lib\site-packages>exit



More information about the Python-list mailing list