Python開発環境の構築

手元のMacBook Proにpythonbrewとvenvを使ってPython開発環境を構築しました。

構成

Mac OS X 10.7.3 ( Snow Leopard )
Xcode 4.3
pythonbrew 1.1
Python 2.7.2, 3.2

環境構築手順

pythonbrewをインストールします。
$ curl -kL http://xrl.us/pythonbrewinstall | bash

2012年8月26日、ダウンロード先のURLをhttp://github.com/utahta/pythonbrew/raw/master/pythonbrew-installから変更しました。

インストールが終わったら.bash_profileに次の一行を追記し、

FILE: $HOME/.bash_profile

[ -s "$HOME/.pythonbrew/etc/bashrc" ] && source "$HOME/.pythonbrew/etc/bashrc"

sourceしておきます。

$ source .bash_profile
Pythonをインストールします。
$ pythonbrew install 2.7.2 3.2

特定のバージョンを有効化するにはswitchコマンドを使用します。

$ pybrew switch 2.7.2
仮想環境を作成します。

作成された仮想環境は使用中のバージョンに紐付けられます。

$ pybrew venv create py272

使用中のバージョンに紐付いた仮想環境を表示します。

$ pybrew venv list
# virtualenv for Python-2.7.2 (found in /Users/tetsuyai/.pythonbrew/venvs/Python-2.7.2)
py272

仮想環境をすべて表示するには-aオプションを追加します。

$ pybrew venv list -a
# virtualenv for Python-2.7.2 (found in /Users/tetsuyai/.pythonbrew/venvs/Python-2.7.2)
py272
# virtualenv for Python-3.2 (found in /Users/tetsuyai/.pythonbrew/venvs/Python-3.2)
py32

仮想環境を選択します。

$ pythonbrew venv use py272
# Using `py272` environment (found in /Users/tetsuyai/.pythonbrew/venvs/Python-2.7.2)
# To leave an environment, simply run `deactivate`

選択した仮想環境を終了します。

$ deactivate

いらなくなった仮想環境は削除しましょう。

$ pythonbrew venv delete py272
Deleting `py272` environment in /Users/tetsuyai/.pythonbrew/venvs/Python-2.7.2

参考リンク

re: pythonbrewで構築するPython開発環境 - @utahta
残念なことに、pythonbrewの積極的なメンテナンスは終了だそうです。
re: pythonbrewの今後とpythonz - @utahta