LoginSignup
6

More than 5 years have passed since last update.

macOS Sierra に pyenv + python3.5 + OpenCV3 をインストール

Posted at

去年秋頃に同じ内容の記事がありますが、今日やってみたら細かいワークアラウンドが不要となっていたので、2017年4月版としてメモを残しておきます。

前提条件

  • Homebrew 導入済み
    • brew update 実行済み
  • macOS Sierra 10.12.3
  • xcode 8.3(8E162)
    • xcode-select --install 実行済み

pyenv のインストール

$ brew install pyenv

~/.bash_profile の先頭に以下の行を追加。

~/.bash_profile
eval "$(pyenv init -)"

python3.5 のインストール

$ pyenv install 3.5.3
$ pyenv global 3.5.3

pyenv install 時に zlib がなくてエラーになる場合は、xcode-select --installが実行されていないので、実行してインストールします。

シェルを開き直す

.bash_profile の変更を反映させるため、ここで一度シェルを終了し、開き直しましょう。

python 3.5を選択し、バージョンチェック

$ python -V
Python 3.5.3

OpenCV3 のインストール

$ brew install opencv3 --with-python3

pyenv から使えるように、リンクを貼る

OpenCV3 のインストールパス配下にある cv2.cpython-35m-darwin.so に対してシンボリックリンクを貼ります。
インストールパスは brew install 後の Caveats に出てきます。私が試した時点でのインストールパスは /usr/local/Cellar/opencv3/3.2.0 でしたので、次のコマンドを実行しました。

$ ln -s /usr/local/Cellar/opencv3/3.2.0/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so .pyenv/versions/3.5.3/lib/python3.5/site-packages/

numpy のインストール

$ pip3 install numpy

動作確認

$ python
Python 3.5.3 (default, Apr  4 2017, 19:19:54) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'

以上です。

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
6