LoginSignup
27
27

More than 5 years have passed since last update.

Mac OS Marvericks にHomebrewを使ってpyenv,pythonの環境設定を行うメモ

Last updated at Posted at 2013-07-25

pythonバージョン管理ツール(pyenv)セットアップ

CPython 2.7系や3.3系、jythonやpypyと言った様々なPythonのバージョンや実装を並列でインストールが可能なツール pyenv をセットアップします。

$ brew install pyenv-virtualenv pyenv

rehashもしてくれるpyenv-pip-rehashもついでにインストールしておきます。

$ git clone https://github.com/yyuu/pyenv-pip-rehash.git ~/.pyenv/plugins/pyenv-pip-rehash

以下に従って、自分のログインシェルに対応した設定ファイル(.bashrc, .zshrc, etc)に記述

To enable shims and autocompletion add to your profile:
  if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

To use Homebrew's directories rather than ~/.pyenv add to your profile:
  export PYENV_ROOT=/usr/local/opt/pyenv

インストール可能なバージョンや実装の一覧を表示

$ pyenv install -l
Available versions:
  2.4
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6

...省略...

  pypy-2.0-src
  pypy-2.0.1
  pypy-2.0.1-src
  pypy-2.0.2
  pypy-2.0.2-src
  pypy-2.1
  pypy-2.1-src
  pypy-2.2
  pypy-2.2-src
  pypy-2.2.1
  pypy-2.2.1-src
  pypy-dev
  pypy3-2.1-beta1
  pypy3-2.1-beta1-src
  pypy3-dev
  stackless-2.7-dev
  stackless-2.7.2
  stackless-3.2-dev
  stackless-3.2.2
  stackless-3.3-dev
  stackless-dev

pythonのインストール

$ pyenv install 2.7.6
$ pyenv install 3.3.3
$ pyenv install pypy-2.2.1

設定結果の確認

$ pyenv versions
* system (set by /Users/tstomoki/.pyenv/version)
  2.7.6
  3.3.3

バージョンの切り替え

#### 現在のシェルのバージョン切り替え
$ pyenv shell 2.7.6
# カレントディレクトリのバージョン切り替え
$ pyenv local 2.7.6
# 全体のバージョン切り替え
$ pyenv global 2.7.6

各パッケージのインストール

主にデータ解析に使用するnumpyやscipy、matplotlib等をインストールしていきたいと思います。

scripy用にgfortranのインストールも合わせて

 $ brew install gfortran
 $ pip install numpy
 $ pip install yamlog
 $ pip install scipy
 $ pip install matplotlib

途中で

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 46: ordinal not in range(128)

のエラーが出ましたがこのページで解決しました。
(~/.pyenv/versions/2.7.6/lib/python2.7/site-packages/sitecustomize.py に以下を記述)

sitecustomize.py
import sys
sys.setdefaultencoding('utf-8')

また、「/System/Library/Frameworks/vecLib.framework/Headers/vBasicOps.h:153:23: error: immintrin.h: No such file or directory」というエラーが出ててscipyインストール失敗したのでこのページから以下をexportすることで解決しました。
Xcodeのheaderの問題だったようです。

$ export CC=clang
$ export CXX=clang
$ export FFLAGS=-ff2c

matplotlibのインストール時に怒られたのでこのページのようにfreetype2のリンクを張りました。

$ ln -s /usr/local/opt/freetype/include/freetype2 /usr/local/include/freetype

動作確認

テストファイルの作成

test.py
from pylab import *
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s, linewidth=1.0)
xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')
grid(True)
savefig("test.png")
show()

実行

$ python test.py

これでサインカーブが出力されればpythonとpipのインストールは終了です。

参考資料

Mac mountain lionにmatplotlibを入れる

27
27
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
27
27