coi munje

How to install Python for Intel chip on M1 Mac

February 06, 2022

TL;DR

To install Python 3.8.7 on M1 mac, for example,

arch -arch x86_64 env PATH=${PATH/\/opt\/homebrew\/bin:/} pyenv install 3.8.7

This is from M1 版と Intel 版の Homebrew を併用するとき pyenv がうまく動かない問題を解決する - Qiita

If you encounter platform compatibility problems during package installation, upgrading pip sometimes resolves it.

$ pip install -U pip

Note that the solution above is valid on a shell running without Rosetta. You can check the architecture with arch command as below. It returns arm64 when running without Rosetta.

$ arch
arm64

Prerequisites

  • Install homebrew both for Arm and x86_64.
  • Set up pyenv

When is this necessary?

When you have to use Python versions that do not have Arm build

For example, Python <=3.8.

When you have to use packages that do not have Arm build

Some Python packages include binaries compiled for each specific platform. If you use such packages and they do not have Arm-compatible binaries, you have to use Intel-compatible Python runtime, even if the Python runtime itself has Arm-compatible version.

Example case

I tried to use streamlit-webrtc with Python 3.9 on M1 Mac.

NOTE: For app development using streamlit-webrtc, see this post.

At first, I installed Python 3.9.3 for Arm via pyenv.

$ arch  # arm64
$ pyenv install 3.9.3
$ pyenv shell 3.9.3
$ python -V  # Python 3.9.3

Then, inside a project directory, I set up a virtual env and installed necessary packages, streamlit and streamlit-webrtc.

$ python -m venv .venv
$ . .venv/bin/activate
$ pip install -U pip
$ pip install streamlit
$ pip install streamlit-webrtc

Next, I created an example app.py file as below (This is a Streamlit app script. See this post for the details),

from streamlit_webrtc import webrtc_streamer

webrtc_streamer(key="sample")

and run the Streamlit app with the command below.

$ streamlit run app.py

Then the following error occurred.

Traceback (most recent call last):
  File "/path/to/python39-m1test/.venv/bin/streamlit", line 5, in <module>
    from streamlit.cli import main
  File "/path/to/python39-m1test/.venv/lib/python3.9/site-packages/streamlit/__init__.py", line 72, in <module>
    from streamlit.delta_generator import DeltaGenerator as _DeltaGenerator
  File "/path/to/python39-m1test/.venv/lib/python3.9/site-packages/streamlit/delta_generator.py", line 62, in <module>
    from streamlit.elements.image import ImageMixin
  File "/path/to/python39-m1test/.venv/lib/python3.9/site-packages/streamlit/elements/image.py", line 25, in <module>
    from PIL import Image, ImageFile
  File "/path/to/python39-m1test/.venv/lib/python3.9/site-packages/PIL/Image.py", line 89, in <module>
    from . import _imaging as core
ImportError: dlopen(/path/to/python39-m1test/.venv/lib/python3.9/site-packages/PIL/_imaging.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_xcb_connect'

It seemed that the compiled binary in the PIL package was not working correctly in this platform.

So I removed the virtual evn and the installed Python runtime once,

$ rm -rf .venv
$ pyenv uninstall 3.9.3

and installed the Python runtime for Intel chip with the command described above.

$ arch -arch x86_64 env PATH=${PATH/\/opt\/homebrew\/bin:/} pyenv install 3.9.3

After that, I followed the same steps to set up venv and install the dependencies.

Finally, everything worked correctly.


  • Buy Me A Coffee
  • Support me on Ko-fi

This site is using Google Analytics.