M1 Mac で x86_64 な Python 環境を構築する
April 22, 2022
つい最近、M1 Mac を使うようになって、普通に Python を使っていたのですが、pip 経由でインストールできないパッケージがあることに気づきました。
具体的には、以下のように poetry でインストール中に azureml-dataprep-native などのインストールが失敗します。
$ poetry installInstalling dependencies from lock filePackage operations: 65 installs, 0 updates, 0 removals• Installing azureml-dataprep-native (38.0.0): FailedRuntimeErrorUnable to find installation candidates for azureml-dataprep-native (38.0.0)at ~/.local/lib/python3.8/site-packages/poetry/installation/chooser.py:72 in choose_for68│69│ links.append(link)70│71│ if not links:→ 72│ raise RuntimeError(73│ "Unable to find installation candidates for {}".format(package)74│ )75│76│ # Get the best link• Installing azureml-dataprep-rslex (2.3.1): FailedRuntimeErrorUnable to find installation candidates for azureml-dataprep-rslex (2.3.1)at ~/.local/lib/python3.8/site-packages/poetry/installation/chooser.py:72 in choose_for68│69│ links.append(link)70│71│ if not links:→ 72│ raise RuntimeError(73│ "Unable to find installation candidates for {}".format(package)74│ )75│76│ # Get the best link• Installing dotnetcore2 (2.1.22): FailedRuntimeErrorUnable to find installation candidates for dotnetcore2 (2.1.22)at ~/.local/lib/python3.8/site-packages/poetry/installation/chooser.py:72 in choose_for68│69│ links.append(link)70│71│ if not links:→ 72│ raise RuntimeError(73│ "Unable to find installation candidates for {}".format(package)74│ )75│76│ # Get the best link
pypi に見に行くと、確かに存在しているのにおかしいなぁ、と。
https://pypi.org/project/azureml-dataprep-native/38.0.0/
調べてみると、どうやら m1 mac の問題みたいということがわかりました。
According to this issue dotnet/core#7122, prefect is using an unsupported dependency that does not have wheels for arm64 macs. The folks over at dotnet/core recommend updating to version 6. According to this page it's LTS and will be supported for a while.
I'm not sure about the azure dataprep package though
My current workaround is to run a docker container that emulates x86, but then I'm limited to only using VS code.
ref: https://github.com/PrefectHQ/prefect/issues/5338
結論から言うと、rosetta により x86_64 向けの python をインストールして使うようにすれば問題解決しました。(詳しいことはよく調べていないですが、rosetta がインストールの translation を行っていて、パフォーマンス上の問題はほとんどないとのこと。)
But that's an x86_64 install of mediapipe that you're running through an emulator, not a native ARM mediapipe? What's the performance penalty of running the emulated version? –
Not through an emulator, the installation is translated by a translation layer in a separate environment (The virtual environment). An emulator would suggest that you'd be only able to use what you installed on the emulator. Which is not the case here. After the installation above you can open the project on PyCharm as you'd usually do. You just have to use the x86_64 version of python (which your machine can run in terminal also). –
Rosetta 2 を利用した x86_64 Python のインストール手順
私はいつも Python version を pyenv、package は poetry で管理しているため、pyenv + poetry な環境を構築するまでの手順を示します。
Rosetta 2 が有効化されたターミナルを開く
- Finder を開く
- Applications > Utilities で Terminal を右クリック
- 右クリックメニューから get-info をクリックして、Open Using Rosetta チェックボックスにチェックを入れる
- 既に Terminal アプリが開いている場合は一回閉じて、新しい Terminal ウィンドウを開く
x86_64 アーキテクチャ向け homebrew のインストール
下記のコマンドで x86_64 アーキテクチャ向け homebrew をインストールする
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"ちなみに arch とは、
man arch
から引用すると、アーキテクチャを指定してユニバーサルバイナリを実行するためのコマンドとのことです。The arch command with no arguments, displays the machine's architecture type.
The other use of the arch command is to run a selected architecture of a universal binary. A universal binary contains code that can run on different architectures.
なおデフォルトで、arm64 版の homebrew は下記のパスに、
$ /opt/homebrew/bin/brew --prefix/opt/homebrewx86_64 版の homebrew は下記のパスにインストールされるみたいです。
$ /usr/local/bin/brew --prefix/usr/localそれぞれのアーキテクチャ用の brew をパスで使い分けて運用すると良さそうです。
x86_64 アーキテクチャ向け pyenv のインストール
pyenv に必要なパッケージをインストール
arm64 brew で既にインストールしていたとしても、x86_64 向けに新たにインストールする必要があリます。
$ arch -x86_64 /usr/local/bin/brew install openssl readline sqlite3 xz zlibx86_64 用の brew 環境変数の設定
$ eval "$(/usr/local/bin/brew shellenv)"pyenv のインストール
$ arch -x86_64 brew install pyenvpython のインストール
今回は、python version 3.8.13 をインストールします。
$ arch -x86_64 pyenv install 3.8.13poetry のインストール
$ arch -x86_64 pip install poetry
以降は、arch -x86_64 は、抜いて良さそうです。
poetry install
まとめ
上記の手順で、無事に冒頭のエラーがなくなり、 poetry install
が通るようになりました。