How to check if Python is running on an M1 mac, even under Rosetta?
Asked Answered
S

3

13

I have python 3.10 code that launches a process but it needs to run a different process if it is running on an M1 Mac.

Is there a way to reliably detect if you are on an M1 Mac even if the python process is running in Rosetta?

I've tried this:

print(sys.platform)

# On Intel silicon:
darwin

# On M1 silicon:
darwin

but it always prints "darwin".

I tried sniffing around in the os.* and sys.* libraries and the best I found was this:

print(os.uname())

# On Intel silicon:
posix.uname_result(sysname='Darwin', nodename='XXX', release='21.5.0', version='Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64', machine='x86_64')

# On M1 silicon:
posix.uname_result(sysname='Darwin', nodename='XXX', release='21.4.0', version='Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101', machine='x86_64')

I assume it returns machine= 'x86_64' on the M1 machine because Python is running in Rosetta? The version field does appear different:

# Intel
version='Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64'

# M1
version='Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101'

Is parsing uname() and looking for "ARM" in the version field the best way to check for M1 silicon if you are running under Rosetta?

Sitton answered 6/7, 2022 at 18:57 Comment(1)
What about platform.processor() ? Which gives me arm on M1 (haven't checked using Rosetta)Grizzled
G
9

Use Python's built-in platform library to determine if a Mac is M1/M2:

import platform

print(platform.processor())

On an M1/M2 Mac --> arm

On an older Mac --> i386

Gorgon answered 8/11, 2022 at 21:26 Comment(4)
i386 build of Python running on m1 reports i386 for meJagatai
This is wrong. I am on an M1 mac and platform.processor() returns i386.Repel
But doesn't i386 return value just mean that you are running python through rosetta emulation on your M1 cpu? My M1 reports arm just as this post describes.Intrauterine
Here's how I tested it: On my 2015 Intel MacBook Pro, I installed Python, and platform.processor() returned i386. On my 2022 M2 MacBook Pro, I installed Python, and platform.processor() returned arm. If that's what people are looking for, there it is.Gorgon
B
7

The only way I was able to do it from Python was to call out to sysctl. This is taken from cmake-macos-rosetta:

Non-Rosetta Python on M1:

>>> import platform, subprocess
>>> platform.processor()
'arm'

# Rosetta would not report arm.

>>> subprocess.run(["sysctl", "-n", "sysctl.proc_translated"])
0

Rosetta Python on M1:

>>> import platform, subprocess
>>> platform.processor()
'i386'

# Rosetta fakes the processor.

>>> subprocess.run(["sysctl", "-n", "sysctl.proc_translated"])
1
CompletedProcess(args=['sysctl', '-n', 'sysctl.proc_translated'], returncode=0)
Birt answered 2/12, 2022 at 11:2 Comment(0)
A
1

You could just check for the Processor Name, and check it that way. The easiest way to get it is by using the cpuinfo module. cpuinfo.get_cpu_info()['brand_raw'] returns a string with the processor brand and name, for example "Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz". If you only want to have "i5-6500", you can just take the 3rd word from the string.

import cpuinfo
cpudata = cpuinfo.get_cpu_info()['brand_raw']
cpuname = cpudata.split(" ")[1]

If you then would print(cpuname), it should only output the processor "name", so in this case i5-6500.

Atombomb answered 6/7, 2022 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.