[英]Windows x64 vs x86: Hardware vs. OS vs. process
I'm having trouble with things breaking based on x86 vs x64 in Python 3 on Windows.我在 Windows 上的 Python 3 中遇到基于 x86 与 x64 的问题。
I need to know if my Python program is running:我需要知道我的 Python 程序是否正在运行:
They are not the same thing (at all!).它们不是一回事(完全不同!)。
AMD64 architecture processors can run either 64 or 32 bit operating systems . AMD64 架构处理器可以运行 64 位或 32 位操作系统。
And 64 bit operating systems can run either 64 or 32 bit processes . 64 位操作系统可以运行 64 位或 32 位进程。
I know that:我知道:
platform.architecture()
returns a string - but which of those 3 does it represent? platform.architecture()
返回一个字符串 - 但它代表了这三个中的哪一个? (The documentation doesn't seem to say.) (sys.maxsize > 2**32)
then I'm in a 64 bit process.(sys.maxsize > 2**32)
那么我在 64 位进程中。 Fine; To forestall the inevitable "why do you care?"为了防止不可避免的“你为什么在乎?” questions, it's because my Python program is automating configuration of Windows - things are in different places on x86 vs x64 Windows, but I don't know in advance if my program will be running on 32 or 64 bit Python.
问题,这是因为我的 Python 程序正在自动配置 Windows - x86 和 x64 Windows 上的情况不同,但我事先不知道我的程序是在 32 位还是 64 位 Python 上运行。
So I need to figure that out.所以我需要弄清楚这一点。
So your actual question is whether the Windows you're running on is x64?所以您的实际问题是您运行的 Windows 是否是 x64? :)
:)
Riffing off this and this , how about扯掉这个和这个,怎么样?
import os
arch = (
os.environ.get("PROCESSOR_ARCHITEW6432") or
os.environ.get("PROCESSOR_ARCHITECTURE")
)
# `arch` should now reliably be `x64` if the system is 64-bit.
I believe this will work, but I haven't tested it on a 32-bit version of Windows:我相信这会起作用,但我还没有在 32 位版本的 Windows 上测试过它:
import sys, os
x64_process = (sys.maxsize > 2**32)
x64_os = os.environ.get('ProgramW6432') is not None
And probably my most important use case - restarting explorer.exe after registry changes:可能是我最重要的用例 - 在注册表更改后重新启动 explorer.exe:
def restartExplorer():
'''Restart explorer'''
do(r'taskkill /f /im explorer.exe')
if x64_os and not x64_process:
do(os.environ['systemroot']+ r'\sysnative\cmd.exe /c start /B explorer.exe') # because this Python is in a 32 bit process
else:
do("start explorer.exe")
I won't give you the implementation of do()
because it's pretty obvious.我不会给你
do()
的实现,因为它很明显。 (But will if somebody asks.) (但如果有人问的话。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.