virtualenvで環境を作る

やっとvirtualenvを理解したのでメモメモ。これでグローバルなところにパッケージをインストールしなくて済む!
環境:Debian wheezy 64bit on VirtualBox

インストール

aptからvirtualenvをインストールします。ついでにpython-devもインストール。

$ su -
# apt-get install python-dev python-virtualenv

これでvirtualenvが使えます。確か依存でeasy_install(setuptools?)とpipも入ったかも。

環境を作る

$ virtualenv --no-site-packages temp

これでカレントディレクトリにtempというディレクトリが生成されます。


temp/binとか見てみるとわかるんですが

$ ls temp/bin
activate      activate.fish     easy_install      pip      python
activate.csh  activate_this.py  easy_install-2.7  pip-2.7

pythonそのものがあったり、easy_installとpipも入ってたりします。

環境を使う

環境を使うために以下を実行します。

$ cd temp
$ source bin/activate
(temp)$

sourceや.でbin/activateを読み込むと、PATHの先頭にtemp/binが追加されます。
なのでeasy_installとかpythonコマンドはtemp/binにあるものが使われます。
パッケージの類いはtemp以下のincludeなのかlibなのかlocalなのかわかりませんが、temp以下にインストールされます。

環境から離れる

bin/activateを読み込んだあと、読み込む前の状態に戻すには以下を実行します。

(temp)$ deactivate
$

これで元に戻りました。

Mercurialをインストールしてみる

試しにMercurialをインストールしてみます。

$ source bin/activate
(temp)$ easy_install mercurial
(temp)$ hg --version
Mercurial Distributed SCM (version 2.6-rc)
(see http://mercurial.selenic.com for more information)

Copyright (C) 2005-2012 Matt Mackall and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

インストールされました。これでdeactivateすると…

(temp)$ deactivate
$ hg --version
-bash: hg: command not found

temp環境にインストールしたので使えなくなっています。


これでRhodeCodeなども環境を汚さない場所にインストールできる!