2

I'm wondering how to find the architecture of the machine python is running on and save it to a string. The only other example I've been able to find works only on Windows, and doesn't even detect the proper architecture if it's Windows on Arm. Any solutions would be greatly appreciated.

Thanks,
NullUsxr

NullUsxr
  • 43
  • 7
  • 1
    What have you tried? Have you seen https://docs.python.org/3/library/platform.html? – Iain Shelvington Apr 24 '22 at 03:23
  • It gives the wrong architecture type when ran on arm. The output of the universal command is `('64bit', '')`, despite my system architecture being aarch64. – NullUsxr Apr 24 '22 at 03:31
  • 1
    Have you explored all the functions in the module? `platform.machine()` or `platform.processor()` seem like they might give you what you want? – Iain Shelvington Apr 24 '22 at 03:37
  • `platform.machine()` surprisingly returns the correct value of 'arm64'! Thank you! – NullUsxr Apr 24 '22 at 03:38
  • Do any of the answers on [Is there a reliable way to determine the system CPU architecture using Python?](https://stackoverflow.com/q/7491391) or [How can I return system information in Python?](https://stackoverflow.com/q/466684) help? or [How to collect current architecture in Python 3, in a way that is portable across Linux, Windows and MacOS?](https://stackoverflow.com/q/64044020) ? – Peter Cordes Apr 24 '22 at 06:52
  • The question title is an obvious duplicate of those, so you need to be specific about *exactly* what you want to know. e.g. for a 32-bit Python interpreter running on a 64-bit-capable system, do you want to know that the python interpreter is running in 32-bit mode (so if you want to load any binary DLLs, they need to match that), or do you want to know if you should start the 32 or 64-bit version of some other executable, if this is a wrapper / launcher for another program. (Applicable to x86-64, or somewhat to ARM64 systems) – Peter Cordes Apr 24 '22 at 06:53
  • I temporarily closed the question so it doesn't collect answers that aren't what you're looking for. If it's not a duplicate of an existing SO question, it can get reopened once you have some specific detail about what you want in various corner cases. – Peter Cordes Apr 24 '22 at 06:55

1 Answers1

0
import platform
print(platform.system())

According to https://docs.python.org/3/library/platform.html#platform.system it says the function would "Returns the system/OS name, such as 'Linux', 'Darwin', 'Java', 'Windows'. An empty string is returned if the value cannot be determined."

hexa
  • 24
  • 5
  • Good idea, though it doesn't tell me if 'Darwin' or 'Windows', etc. is 32-bit, 64-bit, aarch64 and so on. Using `platform.architecture(executable=sys.executable, bits='', linkage='')` from the same source as yours, also returns an invalid value if on the aarch64 architecture. – NullUsxr Apr 24 '22 at 03:34