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

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página