popen / batchfile / environment variables

Guy guy at lightwork.co.uk
Wed Nov 20 10:04:54 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

Both the ways described are good, I would proberly go for the last one
described by logistix at zworg.com. However you can set enviroment vars
in python

As shown below : I use something like this. Its then possible to
execute a bat file with the enviroment that you have set in python
using popen.

the below function is only a quick function that I made, It works but
might need something smoothing round the edges.


ENV_PATH        = ["C:\Program Files\Microsoft Visual
Studio\Common\msdev98",
                   "C:\Program Files\Microsoft Visual
Studio\VC98\BIN",
                   "C:\Program Files\Microsoft Visual TOOLS\WINNT",
                   "C:\Program Files\Microsoft Visual
Studio\Common\TOOLS",
                   "C:\WINNT\SYSTEM"]

AddToEnvroment("PATH",ENV_PATH,";")

# AddToEnviroment - Allows a list of items to be added to an
enviroment var like PATH,INCLUDE,LIB.
def AddToEnvroment(EnviromentVarName,ValuesList=[],Separator=";"):
    # Gets the enviroment Var, and splits into a list.
    TotalEnv =""
    try:
        EnvVarValue=os.environ[EnviromentVarName]
    except:
        os.environ[EnviromentVarName]=""
        EnvVarValue=os.environ[EnviromentVarName]
        
    if EnvVarValue != "":
        EnvVarValue=string.split(EnvVarValue,Separator)
        for Value in ValuesList:
            EnvVarValue.append(Value)
        for Value in EnvVarValue:
            TotalEnv = TotalEnv + Value + Separator
        os.environ[EnviromentVarName]=TotalEnv
    else:       
        for Value in EnvVarValue:
            TotalEnv = TotalEnv + Value + Separator
        os.environ[EnviromentVarName]=TotalEnv



More information about the Python-list mailing list