21

Possible Duplicate:
How can I return system information in Python?

For example, to see if a Solaris is a Solaris X86 or Solaris SPARC?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Jack Z
  • 2,572
  • 4
  • 20
  • 17
  • 3
    What was wrong with http://docs.python.org/library/platform.html? It seems like it does everything you want. What's wrong with it? – S.Lott Sep 20 '11 at 20:31
  • So you googled for python system architecture and didn't find that the first link is to the platform module? – donkopotamus Sep 20 '11 at 21:46
  • 3
    Voting to reopen. https://stackoverflow.com/questions/466684/how-can-i-return-system-information-in-python asks several other parameters, but not specifically CPU architecture. – Ciro Santilli OurBigBook.com Nov 17 '18 at 18:02

2 Answers2

28
>>> import platform
>>> platform.system()
'Darwin'
>>> platform.processor()
'i386'
>>> platform.platform()
'Darwin-10.8.0-i386-64bit'
>>> platform.machine()
'i386'
>>> platform.version()
'Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386'
>>> platform.uname()
('Darwin', 'Hostname.local', '10.8.0', 'Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386', 'i386', 'i386')
Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
15

I used the following:

>>> import platform
>>> platform.uname()
('Darwin', 'Matthew-Rankins-MacBook-Pro.local', '10.8.0', 
'Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; 
root:xnu-1504.15.3~1/RELEASE_X86_64', 'x86_64', 'i386')
>>> 

From the Python platform documentation:

platform.uname()

Fairly portable uname interface. Returns a tuple of strings (system, node, release, version, machine, processor) identifying the underlying platform.

Note that unlike the os.uname() function this also returns possible processor information as additional tuple entry.

Entries which cannot be determined are set to ''.

Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
  • 1
    It returns the wrong information my M1 MacBook Air running Mac OS 11.1. This is what it returns: ('Darwin', 'MacBook-Air.local', '20.2.0', 'Darwin Kernel Version 20.2.0: Wed Dec 2 20:40:21 PST 2020; root:xnu-7195.60.75~1/RELEASE_ARM64_T8101', 'x86_64', 'i386') – user1766438 Sep 19 '21 at 13:29
  • 1
    It looks like the architecture of the Python binary will influence the results from platform.uname(). The information after the word RELEASE can be used to reliably give you the platform's architecture. – user1766438 Sep 19 '21 at 21:52
  • 5
    on macOS, uname `machine` and `processor` will return x86 even on ARM if the process calling it is running via Rosetta. you'll want the `version` part of the uname tuple (or just call `platform.version()`). on Intel you should see `RELEASE_X86_64` and on Apple Silicon you should similar to what @user1766438 posted - `RELEASE_ARM64`. `'ARM' in platform.uname().version` or similar should work as a Python architecture check on macOS. otherwise subprocess'ing `sysctl machdep.cpu.brand_string` is probably your next best bet. – bheinz Dec 16 '21 at 18:12