[python-win32] regarding invoking command prompt using python

Vernon Cole vernondcole at gmail.com
Wed Jun 17 22:54:02 CEST 2009


Everyone:

If I read the request correctly, this question is about how to set
environment variables from a python script.
In other words: what is the Python equivalent of the DOS command:
SET envrmnt="This is the new value"

If my experience is correct (I would love it if someone will tell me that I
am wrong) it cannot be done.

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)

<begin cmd console dump>
C:>c:\python26\python.exe
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.putenv('x','xxx')
>>> os.system('set x')   # run a child process
x=xxx
0
>>> print repr(os.getenv('x'))
None
>>>exit()

C:>set x="zzz"
C:>c:\python26\python.exe
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32api
>>> win32api.SetEnvironmentVariable('X','xxx')
>>> import os
>>> print repr(os.getenv('X'))
'zzz'
>>>
<end console dump>

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.
-------------------------------------------------------------------------------------------------
Dear a h:

The only way I know to permanently set environment variables is:
-Right-click on "My Computer"
-select "Properties"
-select "Advanced System Settings"
-click on "Environment Variables"

Many of us old DOS hackers keep a directory of utility scripts to make
running command-line commands easier.
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.
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".
So when I forget which operating system I am using and type "ls *.txt", it
works.

More important are the Python helpers.
python.bat contains: "c:\python26\python.exe %1 %2 %3 %4 %5 %6 %7 %8 %9"
py30.bat contains: "c:\python30\python.exe %1 %2 %3 %4 %5 %6 %7 %8 %9"
ipy.bat contains: ""c:\program files\Ironpython 2.0.1\ipy.exe" %1 %2 %3 %4
%5 %6 %7 %8 %9"
ped.bat contains: "start
c:\python26\Lib\site-packages\pythonwin\pythonwin.exe %1 %2 %3"
2to3.bat contains: "c:\python26\python.exe
c:\python26\Tools\Scripts\2to3.py  %1 %2 %3 %4 %5 %6 %7 %8 %9"

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:

>python testme.py
>ipy testme.py
>copy testme.py testme3.py
>2to3 testme3.py --write
>py30 testme3.py

Hope this helps a little.
--
Vernon Cole


On Wed, Jun 17, 2009 at 12:54 PM, Tim Roberts <timr at probo.com> wrote:

> a h wrote:
> >
> > thanks for the reply, what i have done to set up the environment
> > variables is open the command prompt and then has given the whole path
> > of c:\..\vsvars32.bat and then enter.
> > A message appear is "setting environment for using MS visual studio".
> > and then using DEVENV i had build my solution.
>
> "devenv" is a fairly heavyweight way to do that.  It basically invokes
> the whole IDE.  If you are using Visual Studio 2005 or 2008, you can say
>    msbuild xxxx.sln
> which is not quite so heavy as devenv.  If you're using an older Visual
> Studio, that may not be available.
>
> > can u please help me out that how do i perform above steps in python.
> > code snippet is required.
>
> I was going to say "just write the commands out to a temporary batch
> file, then call os.system or subprocess.call to run the batch file"
> until I read this followup:
>
> a h wrote:
> >
> > 1.I want to know how do i write batchfile kind of file in python so
> > that it executes some commands. Code snippets are required.
>
> For the future, the way you worded that is impolite.  When you say
> "required", it is as if you are demanding something from us.  It would
> be better to say "Please provide code snippets."
>
>
> > 2.As well how do i invoke command prompt.
> >
> > Actually what i need is that i have to perform certain things in
> > windows...first i have to go to start->run->type cmd
> > then command prompt window is open... in that to run an .exe i have to
> > give the whole path like this c:\> cd
> > temp_folder\next_folder\test.exe... and many more commands...
> > So I want this to be perfomed in python. please help
>
> Can't you just work this out for yourself?  You create a file, you write
> to it, you run it:
>
>    fbat = open( "xxx.bat", "w" )
>    print >> fbat, "echo I'm inside a batch file"
>    print >> fbat, 'call "c:\\Program Files\\Microsoft etc\\vsvars32.bat'"
>    print >> fbat, 'msbuild myproject.sln'
>    fbat.close()
>    os.system( "xxx.bat" )
>    os.remove( "xxx.bat" )
>
> Note that you will need extra quotes when the path includes spaces.
> Note also the escaped backslash characters.
>
> --
> Tim Roberts, timr at probo.com
> Providenza & Boekelheide, Inc.
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20090617/b749e07b/attachment.htm>


More information about the python-win32 mailing list