2011-02-05
複数のバージョンの python 環境を切り替えられる virtualenv
Ruby でいえば rvm みたいなもの。非常にお手軽なので、特に書くことはないのだけど、何についても覚えない私はサンドボックスを切り替えたいと思う度にコマンドがわからなくて調べているので、自分の管理下にもメモ。
まずは環境を準備します。インストールとセットアップ。
virtualenv のインストール
sudo easy_install virtualenv
サンドボックスの作成
pythonのパスと、自分がサンドボックスにしたい任意のディレクトリへのパスを引数に指定します。
virtualenv --python=/usr/bin/python26 ~/virtualenv/python2.6
ここまでやると準備は完了です。これは何度もやる必要はありません。
python2.6に切り替える
CentOS5.5の標準pythonは2.4ですが、その環境はそのままに2.6を使いたい。
source ~/virtualenv/python2.6/bin/activate
source コマンドはみんなが普段から普通に使っているシェルコマンドで*1、文字列をシェルで実行します。.bashrc の内容を書き換えた後に変更を反映するために使う source (もしくは . ) と同じ物です。
つまり bin/activate は、環境の切り替えに必要な処理をする文字列を返してくれるので、あなたのシェルで実行しましょう、という事です。
これを実行して、which python を実行すると python26 のパスが返されるはずです。
「目標をセンターに入れてスイッチ」ごとく「サンドボックスに移動して . bin/activate」だけ覚えておけばいいはずなんだけど、毎回さっぱり忘れてるのです。
さいごに
前述のように環境を変更する sh スクリプトを bin/activate が返し、 source コマンドでロードする、という仕組みなので、どのような処理を行うかは、cat や less などで確認可能です。
less ~/virtualenv/python2.6/bin/activate # This file must be used with "source bin/activate" *from bash* # you cannot run it directly deactivate () { # reset old environment variables if [ -n "$_OLD_VIRTUAL_PATH" ] ; then PATH="$_OLD_VIRTUAL_PATH" export PATH unset _OLD_VIRTUAL_PATH fi if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME" export PYTHONHOME unset _OLD_VIRTUAL_PYTHONHOME fi # This should detect bash and zsh, which have a hash command that must # be called to get it to forget past commands. Without forgetting # past commands the $PATH changes we made may not be respected if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then hash -r fi if [ -n "$_OLD_VIRTUAL_PS1" ] ; then PS1="$_OLD_VIRTUAL_PS1" export PS1 unset _OLD_VIRTUAL_PS1 fi unset VIRTUAL_ENV if [ ! "$1" = "nondestructive" ] ; then # Self destruct! unset -f deactivate fi } # unset irrelavent variables deactivate nondestructive VIRTUAL_ENV="/home/username/virtualenv/python2.6" export VIRTUAL_ENV _OLD_VIRTUAL_PATH="$PATH" PATH="$VIRTUAL_ENV/bin:$PATH" export PATH # unset PYTHONHOME if set # this will fail if PYTHONHOME is set to the empty string (which is bad anyway) # could use `if (set -u; : $PYTHONHOME) ;` in bash if [ -n "$PYTHONHOME" ] ; then _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME" unset PYTHONHOME fi if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then _OLD_VIRTUAL_PS1="$PS1" if [ "x" != x ] ; then PS1="$PS1" else if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then # special case for Aspen magic directories # see http://www.zetadev.com/software/aspen/ PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" else PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" fi fi export PS1 fi # This should detect bash and zsh, which have a hash command that must # be called to get it to forget past commands. Without forgetting # past commands the $PATH changes we made may not be respected if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then hash -r fi
*1:普通に使っているかは知らないけど、.*rc系の設定ファイルの中で設定を分けるために使う人はよく使う
トラックバック - http://d.hatena.ne.jp/MER/20110205/1296902809
リンク元
- 259 http://pipes.yahoo.com/pipes/pipe.info?_id=tDfBdGWF3RGl9XNm1L3fcQ
- 40 http://www.google.co.jp/url?sa=t&rct=j&q=python バージョン 切り替え&source=web&cd=2&ved=0CCUQFjAB&url=http://d.hatena.ne.jp/MER/20110205/1296902809&ei=G0qiTuGgG6XJmAX_1bSZCQ&us
- 30 http://pipes.yahoo.com/pipes/pipe.info?_id=12e453e301454b799b3ac6642aa089b5
- 21 http://www.google.co.jp/url?sa=t&rct=j&q=virtualenv python バージョン&source=web&cd=1&ved=0CBwQFjAA&url=http://d.hatena.ne.jp/MER/20110205/1296902809&ei=13y6TpP5FbGNmQXMoK3tBw&usg=AFQjCNEPRfmgELdbggMTtZ6a
- 20 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=python+virtualenv
- 15 http://www.google.co.jp/url?sa=t&rct=j&q=python 切り替え&source=web&cd=2&ved=0CC4QFjAB&url=http://d.hatena.ne.jp/MER/20110205/1296902809&ei=jxyETtKDNOPemAWJt832Dw&usg=AFQjCNEPRfmgELdbggMTtZ6akJa8Z7CfUw&sig2=cjtx53
- 12 http://www.google.co.jp/url?sa=t&rct=j&q=python+バージョン+切り替え&source=web&cd=3&ved=0CDAQFjAC&url=http://d.hatena.ne.jp/MER/20110205/1296902809&ei=BDPTToXsDojRmAW--t2fDw&usg=AF
- 9 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=virtualenv+python+version&qscrl=1
- 8 http://www.google.co.jp/url?sa=t&rct=j&q=python 複数バージョン 切り替え&source=web&cd=2&ved=0CCkQFjAB&url=http://d.hatena.ne.jp/MER/20110205/1296902809&ei=V
- 6 http://www.google.co.jp/search?aq=f&sourceid=chrome&ie=UTF-8&q=python+切り替え
