Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get wrong value for os.environ["ProgramFiles"] on 64bit vista

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"?

like image 793
Gur Avatar asked Aug 16 '09 07:08

Gur


4 Answers

The only difference between Python x32 and x64 is os.environ["ProgramFiles"]. So if you want to be safe on both Python platforms, use ProgramW6432 or ProgramFiles(x86), but not ProgramFiles.

Python 3 x64 (on Win10 x64):

>>> import os
>>> os.environ["ProgramFiles"]
'C:\\Program Files'
>>> os.environ["ProgramFiles(x86)"]
'C:\\Program Files (x86)'
>>> os.environ["ProgramW6432"]
'C:\\Program Files'
>>> os.environ["ProgramData"]
'C:\\ProgramData'

Python 3 x32:

>>> import os
>>> os.environ["ProgramFiles"]
'C:\\Program Files (x86)'
# The other paths are similar to x64
like image 188
Roi Danton Avatar answered Nov 15 '22 18:11

Roi Danton


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)
>>>
like image 29
ThatGraemeGuy Avatar answered Nov 15 '22 17:11

ThatGraemeGuy


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%.

like image 43
Mark Rushakoff Avatar answered Nov 15 '22 17:11

Mark Rushakoff


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.

like image 23
rob Avatar answered Nov 15 '22 17:11

rob