windows Python 在 64 位 vista 上获取 os.environ["ProgramFiles"] 的错误值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1283664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python get wrong value for os.environ["ProgramFiles"] on 64bit vista
提问by Gur
Python 2.4.3 on a Vista64 machine.
Vista64 机器上的 Python 2.4.3。
The following 2 variables are in the environment:
环境中有以下2个变量:
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"?
知道如何获得“ProgramFiles”的正确值吗?
回答by Mark Rushakoff
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).
%程序文件%
该变量指向 Program Files 目录,该目录存储 Windows 和其他所有已安装的程序。英语语言系统上的默认值是 C:\Program Files。在 64 位版本的 Windows(XP、2003、Vista)中,还有 %ProgramFiles(x86)% 默认为 C:\Program Files (x86) 和 %ProgramW6432% 默认为 C:\Program Files。%ProgramFiles% 本身取决于请求环境变量的进程本身是 32 位还是 64 位(这是由 Windows-on-Windows 64 位重定向引起的)。
So to get just C:\Program Files, you apparently want to check %ProgramW6432%
.
所以为了得到 C:\Program Files,你显然想检查%ProgramW6432%
.
回答by ThatGraemeGuy
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.
您可以安装 Python 2.5.4 并重试吗?更新:我的意思是 2.5.4 的 x64 版本。AFAIK 2.4 仅适用于 Windows x86 和 IA64,不适用于 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.
我在 Win 7 x64 上运行 2.5.4 x64 并且我没有得到相同的结果,但我不确定问题是出在 Python 还是 Vista 上。
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)
>>>
回答by rob
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.
您使用的是 32 位版本的 Python 解释器。当使用 32 位软件时,WOW64 将创建一个新环境,它有自己的文件夹和替换。
You can see what I am talking about just by starting the 64-bit and the 32-bit version of the command prompt:
只需启动 64 位和 32 位版本的命令提示符,您就可以看到我在说什么:
64-bit cmd.exe:
64 位 cmd.exe:
C:\Documents and Settings\Administrator>set prog
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
32-bit cmd.exe:
32 位 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.
从上面的第二个摘录中可以看出,要获取 64 位程序文件,您必须使用ProgramW6432
环境变量。
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.
然而,另一种方法也可以解决将来可能出现的其他问题(尤其是注册表设置!):只使用 64 位版本的 Python - 即使我不知道从哪里下载 2.4 的 64 位版本。
回答by Roi Danton
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 x32 和 x64 之间的唯一区别是os.environ["ProgramFiles"]
. 因此,如果您想在两个 Python 平台上都安全,请使用ProgramW6432
或ProgramFiles(x86)
,但不要使用ProgramFiles
。
Python 3 x64 (on Win10 x64):
Python 3 x64(在 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:
Python 3 x32:
>>> import os
>>> os.environ["ProgramFiles"]
'C:\Program Files (x86)'
# The other paths are similar to x64