views:

715

answers:

3

Python 2.4.3 on a Vista64 machine.

The following 2 variables are in the environment:

ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)

But when I run the following

import os
print os.environ["ProgramFiles"]
print os.environ["ProgramFiles(x86)"]

I get:

C:\Program Files (x86)
C:\Program Files (x86)

Any idea how can I get the correct value of "ProgramFiles"?

+3  A: 

From the Wikipedia page:

%ProgramFiles%

This variable points to Program Files directory, which stores all the installed program of Windows and others. The default on English-language systems is C:\Program Files. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).

So to get just C:\Program Files, you apparently want to check %ProgramW6432%.

Mark Rushakoff
+4  A: 

Can you install Python 2.5.4 and try again? UPDATE: I meant the x64 release of 2.5.4. AFAIK 2.4 was only available for Windows x86 and IA64, not x64.

I'm running 2.5.4 x64 on Win 7 x64 and I don't get the same result, but I'm not sure if the problem lies with Python or Vista in your case.

Python 2.5.4 (r254:67916, Dec 23 2008, 15:19:34) [MSC v.1400 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ["ProgramFiles"]
C:\Program Files
>>> print os.environ["ProgramFiles(x86)"]
C:\Program Files (x86)
>>>
Graeme Donaldson
+2  A: 

You are using the 32-bit version of Python interpreter. When using 32-bit software, WOW64 will create a new environment, with it own folders and substitutions.

You can see what I am talking about just by starting the 64-bit and the 32-bit version of the command prompt:

64-bit cmd.exe:

C:\Documents and Settings\Administrator>set prog
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)

32-bit cmd.exe:

C:\WINDOWS\SysWOW64>set prog
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files

As you can see from the second excerpt above, to get the 64-bit Program Files, you have to use the ProgramW6432 environment variable.

Another approach, however, could solve also other issues that may arise in future (especially with registry settings!): just use the 64-bit version of Python - even if I do not know where to download the 64-bit version of 2.4.

Roberto Liffredo