Why Python native on M1 Max is gre… (2024)

I have worked out a workaround solution - How to install numpy on M1 Max, with the most accelerated performance (Apple's vecLib)? Here's the answer as of Dec 6 2021.

Steps

I. Install miniforge

So that your Python is run natively on arm64, not translated via Rosseta.

  1. Download Miniforge3-MacOSX-arm64.sh, then
  2. Run the script, then open another shell
$ bash Miniforge3-MacOSX-arm64.sh
  1. Create an environment (here I use name np_veclib)
$ conda create -n np_veclib python=3.9$ conda activate np_veclib

II. Install Numpy with BLAS interface specified as vecLib

  1. To compile numpy, first need to install cython and pybind11:
$ conda install cython pybind11
  1. Compile numpy by (Thanks @Marijn's answer) - don't use conda install!
$ pip install --no-binary :all: --no-use-pep517 numpy
  1. An alternative of 2. is to build from source
$ git clone https://github.com/numpy/numpy$ cd numpy$ cp site.cfg.example site.cfg$ nano site.cfg

Edit the copied site.cfg: add the following lines:

[accelerate]libraries = Accelerate, vecLib

Then build and install:

$ NPY_LAPACK_ORDER=accelerate python setup.py build$ python setup.py install
  1. After either 2 or 3, now test whether numpy is using vecLib:
>>> import numpy>>> numpy.show_config()

Then, info like /System/Library/Frameworks/vecLib.framework/Headers should be printed.

III. For further installing other packages using conda

Make conda recognize packages installed by pip

conda config --set pip_interop_enabled true

This must be done, otherwise if e.g. conda install pandas, then numpy will be in The following packages will be installed list and installed again. But the new installed one is from conda-forge channel and is slow.

Comparisons to other installations:

1. Competitors:

Except for the above optimal one, I also tried several other installations

  • A. np_default: conda create -n np_default python=3.9 numpy
  • B. np_openblas: conda create -n np_openblas python=3.9 numpy blas=*=*openblas*
  • C. np_netlib: conda create -n np_netlib python=3.9 numpy blas=*=*netlib*

The above ABC options are directly installed from conda-forge channel. numpy.show_config() will show identical results. To see the difference, examine by conda list - e.g. openblas packages are installed in B. Note that mkl or blis is not supported on arm64.

  • D. np_openblas_source: First install openblas by brew install openblas. Then add [openblas] path /opt/homebrew/opt/openblas to site.cfg and build Numpy from source.
  • M1 and i9–9880H in this post.
  • My old i5-6360U 2cores on MacBook Pro 2016 13in.

2. Benchmarks:

Here I use two benchmarks:

  1. mysvd.py: My SVD decomposition
import timeimport numpy as npnp.random.seed(42)a = np.random.uniform(size=(300, 300))runtimes = 10timecosts = []for _ in range(runtimes): s_time = time.time() for i in range(100): a += 1 np.linalg.svd(a) timecosts.append(time.time() - s_time)print(f'mean of {runtimes} runs: {np.mean(timecosts):.5f}s')
  1. dario.py: A benchmark script by Dario Radečić at the post above.

3. Results:

+-------+-----------+------------+-------------+-----------+--------------------+----+----------+----------+| sec | np_veclib | np_default | np_openblas | np_netlib | np_openblas_source | M1 | i9–9880H | i5-6360U |+-------+-----------+------------+-------------+-----------+--------------------+----+----------+----------+| mysvd | 1.02300 | 4.29386 | 4.13854 | 4.75812 | 12.57879 | / | / | 2.39917 |+-------+-----------+------------+-------------+-----------+--------------------+----+----------+----------+| dario | 21 | 41 | 39 | 323 | 40 | 33 | 23 | 78 |+-------+-----------+------------+-------------+-----------+--------------------+----+----------+----------+
Why Python native on M1 Max is gre… (2024)

FAQs

Does Python run natively on M1 Mac? ›

There are currently three options for running Python on the M1: Use pyenv to create environments and pip to install native macOS ARM64 wheels or build packages from source. Use an x86-64 Python distribution, like Anaconda or conda-forge, with Rosetta2. Use the experimental conda-forge macOS ARM64 distribution.

Does Python work on ARM64? ›

The macOS installers on www.python.org are “Universal 2” builds, which means they support both x86-64 and arm64 natively.

How do I change the Python version on my Mac M1? ›

How to upgrade Python using Terminal
  1. Go to Applications > Utilities and launch Terminal.
  2. Press Return.
  3. Type in your admin password and press Return.
  4. Press Return when requested in the Terminal window.
  5. Wait for Homebrew to install.
Nov 1, 2023

Is MacBook Pro M1 good for Python Programming? ›

Quite seldomly do you just use Python. There are always some extra libraries, databases, cache systems and more. But yes. The m1 is very good for programming.

Does Python run natively on Apple Silicon? ›

macOS used to come with Python 2.7 pre-installed between versions 10.8 and 12.3. You are invited to install the most recent version of Python 3 from the Python website. A current “universal2 binary” build of Python, which runs natively on the Mac's new Apple Silicon and legacy Intel processors, is available there.

Is Python native on macOS? ›

Although it comes preinstalled on Macs, as of macOS 12.3, it is no longer required for the normal functioning of the operating system. This article shows how to install Python on a Mac and set up a flexible coding environment with Visual Studio Code. A computer running macOS.

Does Python need SSD? ›

When developing a python program you would initialize variables, create object, perform arithmetic, boolean and logical operations, and write functions. All of this happens in the RAM of your computer and hardly does having an SSD affect you.

Will Python run on ARM? ›

Python has native support for Windows on Arm . Starting with version 3.11, an official installer is available. The latest version at time of writing is 3.12. 0.

Is ARM64 same as 64-bit? ›

arm64 is the current 64-bit ARM CPU architecture, as used since the iPhone 5S and later (6, 6S, SE and 7), the iPad Air, Air 2 and Pro, with the A7 and later chips. armv7s (a.k.a. Swift, not to be confused with the language of the same name), being used in Apple's A6 and A6X chips on iPhone 5, iPhone 5C and iPad 4.

What Python version is compatible with M1? ›

Install Python 3.9 on macOS 11 M1 (Apple Silicon)

pyenv install Python 3.9. 4 seamlessly on Mac M1 and everything works out of the box.

Where is Python 3.11 installed? ›

How installation works
PlatformStandard installation locationDefault value
Unix (pure)prefix/lib/pythonX.Y/site-packages/usr/local/lib/pythonX.Y/site-packages
Unix (non-pure)exec-prefix/lib/pythonX.Y/site-packages/usr/local/lib/pythonX.Y/site-packages
Windowsprefix\Lib\site-packagesC:\PythonXY\Lib\site-packages

How do I know what version of Python I have Mac M1? ›

To check the Python version on Windows, Mac, or Linux, type "python --version" into PowerShell on Windows, or the Terminal on Linux or Mac. To view the Python 3 version, run "Python3 --version" instead.

What is the best Mac for Python programming? ›

The best laptop for coding we've tested is the Apple MacBook Pro 14 (M3, 2023). This premium workstation has a sturdy all-aluminum build, a compact design, and all-day battery life.

Is M1 Pro enough for coding? ›

In conclusion, the M1 is a great choice for most programmers, offering a good balance of performance and value. The M1 Pro is a good choice for developers who need a little extra performance, while the M1 Max and M1 Ultra are best for developers who need the absolute best performance.

What is the best OS for Python development? ›

Linux is a fast, efficient, and lightweight OS. Source codes and scripts run faster in Linux, that's why it is the best operating system for programmers.

Can Python run on MacBook air M1? ›

After buying an M1 Mac, I realized how confusing is to properly set up Python with all data science packages (and non-data science packages) on the new Mac models. According to this long Anaconda guide to the Apple Silicon, there are 3 options for running Python on the M1 — pyenv, anaconda, and miniforge.

Does Python run natively on iOS? ›

Python for iOS and iPadOS

Pyto also provides a complete development environment for running Python 3 including many third-party libraries and system integration on an iPad or iPhone.

Can I use Python on MacBook Air M1? ›

For an Intel based Macbook Air, you'll be fine. How do I install Python packages on a Mac with an M1 processor? Navigate to Anaconda and press the "Get started" button. A list will appear; select "Download Anaconda installers." Anaconda installers for all operating systems can be found here.

How to install Python in Mac M1? ›

Install Python 3 with the Official Installer

First, download an installer package from the Python website. To do that, visit https://www.python.org/downloads/ on your Mac; it detects your operating system automatically and shows a big button for downloading the latest version of Python installer on your Mac.

Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 5458

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.