Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 794
Eric Zinda Avatar asked Jan 25 '26 15:01

Eric Zinda


1 Answers

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

like image 189
Michael Mintz Avatar answered Jan 28 '26 04:01

Michael Mintz