nihiko’s blog

新社会人の雑感

MacにPythonの開発環境を構築する

環境

macOS Mojave(10.14.2)

1.pyenvのインストール

pyenvとは

Linuxにおいて、Pythonの実行環境を管理するツール

$ brew install pyenv

homeblewでpyenvをインストールしようとしたところ、問題①②が出現。

問題①

下記エラーが発生。

$ brew install pyenv  
Error: An exception occurred within a child process:
CompilerSelectionError: autoconf cannot be built with any available compilers.
Install GNU's GCC
brew install gcc

brew install gccをしても同様のエラーが生じる。

原因

gccがhomebrewに認識されてないので、gccのinstallに必要なパッケージもコンパイルできない。

解決策

homebrewにgccが認識されていないため,~/.linuxbrew/binに既にインストールされているgccへのsymbolic linkを作って認識されるようにする。

そこでまた問題

$ gcc -dumpversion
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

原因

MacのOSをアップデートしたことによりコマンドラインツールが抜けてしまっている。

解決策

Xcodeコマンドラインツールをインストール。

xcode-select --install

gccのバージョンを調べる。

gcc -dumpversion
4.2.1

インストールされているgccへのシンボリックリンクを貼る

n -s `which gcc` `brew --prefix`/bin/gcc-4.2.1

問題②

下記エラーが発生。

Error: No such file or directory @ dir_chdir - /usr/local/Cellar
Warning: Bottle installation failed: building from source.

解決策

brew installに失敗した時はとりあえずbrew doctorで原因を探るらしい。

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: The following directories do not exist:
/usr/local/include
/usr/local/lib
/usr/local/opt
/usr/local/sbin
/usr/local/Cellar

You should create these directories and change their ownership to your account.
  sudo mkdir -p /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/Cellar
  sudo chown -R $(whoami) /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/Cellar

ここに書いてあるコマンドを実行すれば

$ pyenv -v
pyenv 1.2.8

行けた。

2. pyenvの設定

.bash.profileに設定を追加。

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

3.pythonのインストール

インストール可能なバージョンを確認。

$ pyenv install -list
Available versions:
  2.1.3
  2.2.3

3.6.5をインストール。しようとしたが、

$ pyenv install 3.6.5
zipimport.ZipImportError: can't decompress data; zlib not available

エラーが発生。

原因

xcode-selectの最新バージョンにMojave用のmacOS SDK headerがデフォルトで入っていない。

解決策
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

すると

$ python --version
Python 3.6.5

いけた。

参考

https://wa3.i-3-i.info/word13504.html

qiita.com

qiita.com

qiita.com