2010年7月14日

Snow Leopard で Python 2.7 を Build する

Python 2.7 の Snow Leopard でのビルド手順メモ。

SQLite のコンパイル。Snow Leopard には最初から SQLite インストールされているけれど、fts3 が有効になってないので、有効にしたものをビルド。

curl -O http://www.sqlite.org/sqlite-3.6.23.1.tar.gz
tar xvfz sqlite-3.6.23.1.tar.gz
cd sqlite-3.6.23.1
CFLAGS="-DSQLITE_ENABLE_FTS3=1" ./configure
make
sudo make install

readline と gettext は独自ビルドしないで fink とか MacPorts とかでインストール。

# fink
# sudo fink selfupdate # 必要に応じて更新
sudo fink install readline
sudo fiink install gettext

# MacPorts
# sudo port -v selfupdate # 必要に応じて更新
# sudo port upgrade outdated # 必要に応じて更新
sudo port install readline
sudo port install gettext

Python のビルド。

curl -O http://www.python.org/ftp/python/2.7/Python-2.7.tar.bz2
tar xvfj Python-2.7.tar.bz2
cd Python-2.7
# fink の場合
./configure --enable-framework MACOSX_DEPLOYMENT_TARGET=10.6 --with-universal-archs=intel --enable-universalsdk=/ --with-threads CPPFLAGS=-I/sw/include LDFLAGS=-L/sw/lib
# MacPorts の場合
./configure --enable-framework MACOSX_DEPLOYMENT_TARGET=10.6 --with-universal-archs=intel --enable-universalsdk=/ --with-threads CPPFLAGS=-I/opt/local/include LDFLAGS=-L/opt/local/lib
make
find . -name readline.so
sudo make install

以下のようなエラーなら問題ないです。

Failed to find the necessary bits to build these modules:
bsddb185           dl                 imageop
linuxaudiodev      ossaudiodev        spwd
sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

_locale や readline がエラーになる場合は主に適切にコンパイルされていないなどが原因と思われます。

特に _locale が上手くコンパイルできない場合は、既存に存在する物をコピーしても動作はします。

sudo cp /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/_locale.so /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/

virtual-python をセットアップします。ここでは $HOME/local 以下に設定しています。

curl -O http://peak.telecommunity.com/dist/virtual-python.py
python2.7 virtual-python.py --prefix=$HOME/local/

~/local/bin にパスを通しておくのを忘れないでください。

export PATH=${HOME}/local/bin:${PATH}
export PYTHONPATH=${HOME}/local/lib/python2.7/site-packages

virtual-python で easy_setup を利用するための設定をします。 ~/.pydistutils.cfg に設定を以下のように記述します。

[install]
install_lib = ~/local/lib/python2.7/site-packages
install_scripts = ~/local/bin

easy_install をセットアップします。

python ez_setup.py

easy_install を利用していくつかライブラリをインストールしておきます。とりあえず、思いついた物だけ例を書いときます。

easy_install -UZ readline
easy_install -UZ mercurial
echo "/opt/subversion/lib/svn-python" >  ~/local/lib/python2.7/site-packages/svn-python.pth
easy_install -UZ hgsubversion
easy_install -UZ hg-git
easy_install -UZ docutils
easy_install -UZ Sphinx
easy_install -UZ ipython
sudo port install libffi
easy_install -UZ pyobjc
easy_install -UZ pyobjc-framework-DictionaryServices
easy_install -UZ Cython
sudo port install libxml2
sudo port install libxslt
easy_install -UZ lxml
easy_install -UZ trac
easy_install -UZ chardet
easy_install -UZ TracAccountManager
easy_install -UZ http://svn.edgewall.com/repos/trac/plugins/0.12/mercurial-plugin
easy_install -UZ web.py
easy_install -UZ SQLAlchemy
easy_install -UZ numpy
easy_install -UZ reportlab
easy_install -UZ rst2pdf

easy_install だと目的通りにインスールできない物は setup.py でインストールします。

curl -O http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz
tar xvfz Imaging-1.1.7.tar.gz
cd Imaging-1.1.7
vi setup.py
# 編集内容
# FREETYPE_ROOT =  libinclude("/Developer/SDKs/MacOSX10.6.sdk/usr/X11/")
# JPEG_ROOT = libinclude("/opt/local/lib/")
# LCMS_ROOT = libinclude("/opt/local/lib/")
python setup.py build
python setup.py install
# http://sourceforge.net/projects/epydoc/files/epydoc/3.0.1/epydoc-3.0.1.tar.gz/download
tar epydoc-3.0.1.tar.gz
cd epydoc-3.0.1
python setup.py build
python setup.py install
# http://sourceforge.net/projects/translate/files/Translate%20Toolkit/1.7.0/translate-toolkit-1.7.0.tar.bz2/download
tar xvfz translate-toolkit-1.7.0.tar.bz2
cd translate-toolkit-1.7.0
python setup.py build
python setup.py install

ついでなので mod_wsgi もビルド。

curl -O http://modwsgi.googlecode.com/files/mod_wsgi-3.2.tar.gz
tar xvfz mod_wsgi-3.2.tar.gz
cd mod_wsgi-3.2
./configure
make
sudo make install

apache は再起動しておきましょう。

sudo apachectl restart