LoginSignup
1
0

More than 1 year has passed since last update.

M1 Mac (macOS Monterey 12.2.1)でpyenv/Python開発環境構築

Posted at

はじめに

先日M1のMBPを入手したので、早速Pythonの開発環境を構築しました。
Inter Macと比べて大きく違うので、昔メモったInter Macの環境構築手順が使えなくなり、新しく環境構築の記事を書こうと思いました。
少しでもお役に立てれば幸いです。

この記事では

Homebrewは使わないで!(動くけど難易度高い)
pyenvは使わないで!(動かない=2021年2月時点)

と記述していましたが、おそらく2021年2月時点ではHomebrewやpyenvなどはまだM1 Macに対応していない(?)と考えられます。現在(2022年4月)では、少し設定は必要ですが、問題なく動作できるようになりました。

Homebrew

まずはHomebrewをインストールします

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)

M1 MacではHomebrewによってインストールされたパッケージは/usr/local/bin/hogeではなく、/opt/homebrew/bin/hoge保存されるので、brewをインストールした後は環境変数を設定する必要があります。

以下を.bashrc.zshrcに追加して、ターミナルを再起動すると、brew installは問題なく動作します。

export PATH=/opt/homebrew/bin:$PATH
export PATH=/opt/homebrew/sbin:$PATH

fishユーザはこれを実行してください。

set PATH /opt/homebrew/bin /opt/homebrew/sbin $PATH

pyenv

pyenvvirtualenvをインストールします

brew install pyenv
brew install pyenv-virtualenv

ここまでは問題ないはずです。

続いてバージョンしてPythonをインストールします。
たとえば Python 3.8.12

pyenv install 3.8.12

しかし、うまくいきません

pyenv install 3.8.12
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Installing Python-3.8.12...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 12.2.1 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/f1/3g092d_11zl8xnf67bp0n79r0000gp/T/python-build.20220318172446.23811
Results logged to /var/folders/f1/3g092d_11zl8xnf67bp0n79r0000gp/T/python-build.20220318172446.23811.log

Last 10 log lines:
checking for --with-cxx-main=<compiler>... no
checking for clang++... no
configure:

  By default, distutils will build C++ extension modules with "clang++".
  If this is not intended, then set CXX on the configure command line.
  
checking for the platform triplet based on compiler characteristics... darwin
configure: error: internal configure error for the platform triplet, please file a bug report
make: *** No targets specified and no makefile found.  Stop.

いろいろ調べた結果、
https://issueantenna.com/repo/pyenv/pyenv/issues/2284

It's due to the latest Apple CLang's releases being incompatible with CPython's Configure.

アップル側が互換性を破壊するリリースがあったようです..!?

Fixed in 3.7.13, 3.8.13, 3.9.11 and 3.10.3 .

上記のバージョンだと問題なく動作します。
大人しくバージョン3.8.13にしたら成功しました。

pyenv install 3.8.13

残りは簡単ですね

pyenv virtualenv 3.8.13 {好きな名前}
pyenv local {好きな名前}

以上です。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0