Everyone:<br><br>If I read the request correctly, this question is about how to set environment variables from a python script.<br>In other words: what is the Python equivalent of the DOS command: <br>SET envrmnt="This is the new value"<br>
<br>If my experience is correct (I would love it if someone will tell me that I am wrong) it cannot be done.<br><br>There are two modules which look like they should work, but they only affect processes which are children of the python code, not the environment from which it is running. (See the console trace below)<br>
<br><begin cmd console dump><br>C:>c:\python26\python.exe<br>Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32<br>Type "help", "copyright", "credits" or "license" for more information.<br>
>>> import os<br>>>> os.putenv('x','xxx')<br>>>> os.system('set x') # run a child process<br>x=xxx<br>0<br>>>> print repr(os.getenv('x'))<br>None<br>>>>exit()<br>
<br>C:>set x="zzz"<br>C:>c:\python26\python.exe<br>Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32<br>Type "help", "copyright", "credits" or "license" for more information.<br>
>>> import win32api<br>>>> win32api.SetEnvironmentVariable('X','xxx')<br>>>> import os<br>>>> print repr(os.getenv('X'))<br>'zzz'<br>>>><br><end console dump><br>
<br>There MUST be some api to actually do what a SET command does, otherwise the CMD shell itself could not do it, but I have never seen any documentation for it.
This seem to me to be the only thing which keeps one from running Python as a command shell.<br>-------------------------------------------------------------------------------------------------<br>Dear a h:<br><br>The only way I know to permanently set environment variables is:<br>
-Right-click on "My Computer"<br>-select "Properties"<br>-select "Advanced System Settings"<br>-click on "Environment Variables"<br><br>Many of us old DOS hackers keep a directory of utility scripts to make running command-line commands easier.<br>
I am in the habit of creating a C:\utils folder and putting all of my quick scripts in there. Then I go through the Windows steps (as above) and edit my PATH environment variable to concatenate ";C:\utils" to the end of whatever is already there. <br>
Into the C:\utils folder I place a few handy filters, such as Tail.exe, touch.exe, and less.exe. Mosly it contains a bunch of .bat files. For example, ls.bat contains only one line of text, which is: "dir %1 %2 %3 %4 %5 %6 %7 %8 %9".<br>
So when I forget which operating system I am using and type "ls *.txt", it works.<br><br>More important are the Python helpers. <br>python.bat contains: "c:\python26\python.exe %1 %2 %3 %4 %5 %6 %7 %8 %9"<br>
py30.bat contains: "c:\python30\python.exe %1 %2 %3 %4 %5 %6 %7 %8 %9"<br>ipy.bat contains: ""c:\program files\Ironpython 2.0.1\ipy.exe" %1 %2 %3 %4 %5 %6 %7 %8 %9"<br>ped.bat contains: "start c:\python26\Lib\site-packages\pythonwin\pythonwin.exe %1 %2 %3"<br>
2to3.bat contains: "c:\python26\python.exe c:\python26\Tools\Scripts\2to3.py %1 %2 %3 %4 %5 %6 %7 %8 %9"<br><br>Suppose I am running a console cmd window in a directory which contains the script "testme.py" and I want to test under different environments. I type:<br>
<br>>python testme.py<br>>ipy testme.py<br>>copy testme.py testme3.py<br>>2to3 testme3.py --write<br>>py30 testme3.py<br><br>Hope this helps a little.<br>--<br>Vernon Cole<br><br><br><div class="gmail_quote">
On Wed, Jun 17, 2009 at 12:54 PM, Tim Roberts <span dir="ltr"><<a href="mailto:timr@probo.com">timr@probo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
a h wrote:<br>
><br>
> thanks for the reply, what i have done to set up the environment<br>
> variables is open the command prompt and then has given the whole path<br>
> of c:\..\vsvars32.bat and then enter.<br>
> A message appear is "setting environment for using MS visual studio".<br>
> and then using DEVENV i had build my solution.<br>
<br>
"devenv" is a fairly heavyweight way to do that. It basically invokes<br>
the whole IDE. If you are using Visual Studio 2005 or 2008, you can say<br>
msbuild xxxx.sln<br>
which is not quite so heavy as devenv. If you're using an older Visual<br>
Studio, that may not be available.<br>
<br>
> can u please help me out that how do i perform above steps in python.<br>
> code snippet is required.<br>
<br>
I was going to say "just write the commands out to a temporary batch<br>
file, then call os.system or subprocess.call to run the batch file"<br>
until I read this followup:<br>
<div class="im"><br>
a h wrote:<br>
><br>
> 1.I want to know how do i write batchfile kind of file in python so<br>
> that it executes some commands. Code snippets are required.<br>
<br>
</div>For the future, the way you worded that is impolite. When you say<br>
"required", it is as if you are demanding something from us. It would<br>
be better to say "Please provide code snippets."<br>
<div class="im"><br>
<br>
> 2.As well how do i invoke command prompt.<br>
><br>
> Actually what i need is that i have to perform certain things in<br>
> windows...first i have to go to start->run->type cmd<br>
> then command prompt window is open... in that to run an .exe i have to<br>
> give the whole path like this c:\> cd<br>
> temp_folder\next_folder\test.exe... and many more commands...<br>
> So I want this to be perfomed in python. please help<br>
<br>
</div>Can't you just work this out for yourself? You create a file, you write<br>
to it, you run it:<br>
<br>
fbat = open( "xxx.bat", "w" )<br>
print >> fbat, "echo I'm inside a batch file"<br>
print >> fbat, 'call "c:\\Program Files\\Microsoft etc\\vsvars32.bat'"<br>
print >> fbat, 'msbuild myproject.sln'<br>
fbat.close()<br>
os.system( "xxx.bat" )<br>
os.remove( "xxx.bat" )<br>
<br>
Note that you will need extra quotes when the path includes spaces.<br>
Note also the escaped backslash characters.<br>
<font color="#888888"><br>
--<br>
Tim Roberts, <a href="mailto:timr@probo.com">timr@probo.com</a><br>
Providenza & Boekelheide, Inc.<br>
</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
python-win32 mailing list<br>
<a href="mailto:python-win32@python.org">python-win32@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-win32" target="_blank">http://mail.python.org/mailman/listinfo/python-win32</a><br>
</div></div></blockquote></div><br>