深いこと考えずにpypyを揉む


pypyという大変coolなプロジェクトが有ります。

PythonそのものによるPython処理系」という、一見よくわからない取り組みですが驚く事にJITコンパイルを搭載しています。
お陰でC実装のPythonよりも高速に実行できるという驚きの処理系です。

動かすには
http://pypy.org/download.html
に飛んでコンパイル済みのバイナリを配ってるのでダウンロードして起動

$ tar xvf pypy-1.6-linux64.tar.bz2
$ cd pypy-1.6/bin
$ ./pypy
Python 2.7.1 (d8ac7d23d3ec, Aug 17 2011, 11:51:19)
[PyPy 1.6.0 with GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information
And now for something completely different: `` good, tests are useful
sometimes :-)''

あっさり立ち上がります。

このままだと使いにくいので/usr/bin/以下にでも置いておきたいのですが
システムの何か大事な物がPythonに依存していたりすると惨事に見舞われます。
CentOSで使われているyumはpython2.4に依存しているらしくGAEの為に2.5に上げたらyumが動かなくなったり

pypyで遊びたいけれどシステムは壊したくない という人はvirtualenvを使いましょう。
以下細かい設定など
適当な場所にpypyを移しておきます。実行させたい場所にシンボリックリンク。なおpythonはbin以下から../を覗いて走るので、実行ファイル単体を取り回しても動きません。

$ sudo mv pypy-1.6 /opt
$ sudo ln -s /opt/pypy-1.6/bin/pypy /usr/bin/pypy

肝心のvirtualenvですが
http://labs.unoh.net/2009/12/2009python.html
http://doc.pypy.org/en/latest/getting-started.html
このあたりを参考に

$ sudo easy_install virtualenv
$ sudo easy_install virtualenvwrapper

とやって揃えると/etc/bash_completion.d/virtualenvwrapperに必要なものが入ったようなので.bashrcなどに

# python virtualenv
export WORKON_HOME=$HOME/.virtualenvs
if [ -f /etc/bash_completion.d/virtualenvwrapper ]; then
    source /etc/bash_completion.d/virtualenvwrapper
fi

と書いてsource .bashrcすると必要なコマンドが使えるようになります
mkvirtualenv(新しく環境を作る), workon(環境を切り替える), deactivate(元の環境に戻る), rmvirtualenv(環境を消す)などなどです。

$ mkvirtualenv -p /usr/bin/pypy pypy
$ workon pypy
(pypy)$ python --version
Python 2.7.1 (d8ac7d23d3ec, Aug 17 2011, 11:51:19)
[PyPy 1.6.0 with GCC 4.4.3]
(pypy)$ python
Python 2.7.1 (d8ac7d23d3ec, Aug 17 2011, 11:51:19)
[PyPy 1.6.0 with GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``topics are for the feeble
minded''
>>>> 3+2
5

よさそうです。