Mac OS X Lion / homebrew にて python の環境を整える

Mac OS X を Lion にアップデートしたら、python の環境が壊れたので再構築。 ちなみに、homebrew は Snow Leopard 時代ですでにインストール済みで、Lionのupgrade した後でも特に問題はなさそうであった。

ポイント

  • Mac OS X の標準 python は /usr/bin/python
  • homebrew の場合は /usr/local が prefix で、 brew insatall python すると /usr/local/Cellar/python/{バージョン番号} がインストールされる。
  • homebrew の python アプリケーションは /usr/local/share/python ディレクトリ配下にインストールされる。

手順

1. brew の更新と python のインストール

$ brew update
$ brew upgrade
$ brew install python
==> Downloading http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
File already downloaded in /Users/joey/Library/Caches/Homebrew
==> Patching
patching file Lib/whichdb.py
Hunk #1 succeeded at 91 with fuzz 1.
==> ./configure --prefix=/usr/local/Cellar/python/2.7.2 --enable-shared
==> make
==> make install
==> Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0
######################################################################## 100.0%
==> /usr/local/Cellar/python/2.7.2/bin/python setup.py install
==> Caveats
A "distutils.cfg" has been written, specifing the install-scripts folder as:
  /usr/local/share/python

If you install Python packages via "python setup.py install", easy_install, pip,
any provided scripts will go into the install-scripts folder above, so you may
want to add it to your PATH.

Distribute has been installed, so easy_install is available.
To update distribute itself outside of Homebrew:
    /usr/local/share/python/easy_install pip
    /usr/local/share/python/pip install --upgrade distribute

See: https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
==> Summary
/usr/local/Cellar/python/2.7.2: 4802 files, 80M, built in 79 seconds

2. pipの再インストール

$ /usr/local/share/python/easy_install pip
Searching for pip
Reading http://pypi.python.org/simple/pip/
Reading http://www.pip-installer.org
Reading http://pip.openplans.org
Best match: pip 1.0.2
Downloading http://pypi.python.org/packages/source/p/pip/pip-1.0.2.tar.gz#md5=47ec6ff3f6d962696fe08d4c8264ad49
Processing pip-1.0.2.tar.gz
Running pip-1.0.2/setup.py -q bdist_egg --dist-dir /var/folders/m1/0m9twrts36n2kj9f1jc07szc0000gn/T/easy_install-1bLHyv/pip-1.0.2/egg-dist-tmp-_2TLlH
warning: no files found matching '*.html' under directory 'docs'
warning: no previously-included files matching '*.txt' found under directory 'docs/_build'
no previously-included directories found matching 'docs/_build/_sources'
Adding pip 1.0.2 to easy-install.pth file
Installing pip script to /usr/local/share/python
Installing pip-2.7 script to /usr/local/share/python

Installed /usr/local/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip

3. 古い環境の掃除と path の変更

$ cd /usr/local/bin
$ mkdir oldPython
$ mv *.py ./oldPython
$ mv pip ./oldPython
$ mv easy_install ./oldPython
 … その他古い python 関連のファイルを移動しておく
$ cd
$ vi .bash_profile
path に /usr/local/share/python を追加しておく (以下の様な感じになった)
PATH=/usr/local/bin:/usr/local/share/python:$PATH:/Applications/eclipse/android-sdk-mac_x86/tools
そして環境を確認する
$ which python
/usr/local/bin/python
$ which pip
/usr/local/bin/pip
$ which easy_install
/usr/local/bin/easy_install

4. sphinx とかを再インストールしてみる

$ pip install sphinx
Downloading/unpacking sphinx
  Downloading Sphinx-1.1.2.tar.gz (2.6Mb): 2.6Mb downloaded
  Running setup.py egg_info for package sphinx

    no previously-included directories found matching 'doc/_build'
Downloading/unpacking Pygments>=1.2 (from sphinx)
  Downloading Pygments-1.4.tar.gz (3.5Mb): 3.5Mb downloaded
  Running setup.py egg_info for package Pygments

Downloading/unpacking Jinja2>=2.3 (from sphinx)
  Downloading Jinja2-2.6.tar.gz (389Kb): 389Kb downloaded
  Running setup.py egg_info for package Jinja2

    warning: no previously-included files matching '*' found under directory 'docs/_build'
    warning: no previously-included files matching '*.pyc' found under directory 'jinja2'
    warning: no previously-included files matching '*.pyc' found under directory 'docs'
    warning: no previously-included files matching '*.pyo' found under directory 'jinja2'
    warning: no previously-included files matching '*.pyo' found under directory 'docs'
Downloading/unpacking docutils>=0.7 (from sphinx)
  Downloading docutils-0.8.1.tar.gz (1.5Mb): 1.5Mb downloaded
  Running setup.py egg_info for package docutils

    warning: no files found matching 'MANIFEST'
    warning: no previously-included files matching '.cvsignore' found under directory '*'
    warning: no previously-included files matching '*.pyc' found under directory '*'
    warning: no previously-included files matching '*~' found under directory '*'
    warning: no previously-included files matching '.DS_Store' found under directory '*'
Installing collected packages: sphinx, Pygments, Jinja2, docutils
  Running setup.py install for sphinx

    no previously-included directories found matching 'doc/_build'
    Installing sphinx-apidoc script to /usr/local/share/python
    Installing sphinx-build script to /usr/local/share/python
    Installing sphinx-quickstart script to /usr/local/share/python
    Installing sphinx-autogen script to /usr/local/share/python
  Running setup.py install for Pygments

    Installing pygmentize script to /usr/local/share/python
  Running setup.py install for Jinja2

    warning: no previously-included files matching '*' found under directory 'docs/_build'
    warning: no previously-included files matching '*.pyc' found under directory 'jinja2'
    warning: no previously-included files matching '*.pyc' found under directory 'docs'
    warning: no previously-included files matching '*.pyo' found under directory 'jinja2'
    warning: no previously-included files matching '*.pyo' found under directory 'docs'
  Running setup.py install for docutils
    changing mode of build/scripts-2.7/rst2html.py from 644 to 755
    changing mode of build/scripts-2.7/rst2s5.py from 644 to 755
    changing mode of build/scripts-2.7/rst2latex.py from 644 to 755
    changing mode of build/scripts-2.7/rst2xetex.py from 644 to 755
    changing mode of build/scripts-2.7/rst2man.py from 644 to 755
    changing mode of build/scripts-2.7/rst2xml.py from 644 to 755
    changing mode of build/scripts-2.7/rst2pseudoxml.py from 644 to 755
    changing mode of build/scripts-2.7/rstpep2html.py from 644 to 755
    changing mode of build/scripts-2.7/rst2odt.py from 644 to 755
    changing mode of build/scripts-2.7/rst2odt_prepstyles.py from 644 to 755

    warning: no files found matching 'MANIFEST'
    warning: no previously-included files matching '.cvsignore' found under directory '*'
    warning: no previously-included files matching '*.pyc' found under directory '*'
    warning: no previously-included files matching '*~' found under directory '*'
    warning: no previously-included files matching '.DS_Store' found under directory '*'
    changing mode of /usr/local/share/python/rst2html.py to 755
    changing mode of /usr/local/share/python/rst2latex.py to 755
    changing mode of /usr/local/share/python/rst2man.py to 755
    changing mode of /usr/local/share/python/rst2odt.py to 755
    changing mode of /usr/local/share/python/rst2odt_prepstyles.py to 755
    changing mode of /usr/local/share/python/rst2pseudoxml.py to 755
    changing mode of /usr/local/share/python/rst2s5.py to 755
    changing mode of /usr/local/share/python/rst2xetex.py to 755
    changing mode of /usr/local/share/python/rst2xml.py to 755
    changing mode of /usr/local/share/python/rstpep2html.py to 755
Successfully installed sphinx Pygments Jinja2 docutils
Cleaning up…

5. pip で PIL をインストールしようとしても失敗する

どうも PIL をダウンロードできない様だ。後日試す事にする。

$ pip install pil -v
Downloading/unpacking pil
  Could not fetch URL http://www.pythonware.com/products/pil (from http://pypi.python.org/simple/PIL/): timed out
  Will skip URL http://www.pythonware.com/products/pil when looking for download links for pil
  Could not fetch URL http://effbot.org/downloads/#Imaging (from http://pypi.python.org/simple/PIL/): timed out
  Will skip URL http://effbot.org/downloads/#Imaging when looking for download links for pil
  Could not fetch URL http://www.pythonware.com/products/pil/ (from http://pypi.python.org/simple/PIL/): timed out
  Will skip URL http://www.pythonware.com/products/pil/ when looking for download links for pil
  Could not fetch URL http://www.pythonware.com/downloads/Imaging-1.1.3.tar.gz (from http://pypi.python.org/simple/PIL/): timed out
  Will skip URL http://www.pythonware.com/downloads/Imaging-1.1.3.tar.gz when looking for download links for pil
  Could not fetch URL http://effbot.org/zone/pil-changes-115.htm (from http://pypi.python.org/simple/PIL/): timed out
  Will skip URL http://effbot.org/zone/pil-changes-115.htm when looking for download links for pil
  Could not find any downloads that satisfy the requirement pil
No distributions at all found for pil
Exception information:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/basecommand.py", line 126, in main
    self.run(options, args)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/commands/install.py", line 223, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/req.py", line 954, in prepare_files
    url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg/pip/index.py", line 152, in find_requirement
    raise DistributionNotFound('No distributions at all found for %s' % req)
DistributionNotFound: No distributions at all found for pil

Storing complete log in /Users/joey/.pip/pip.log

6. pip で PIL をインストールするときの注意点

後日ためしたところ上手く行ったが、FREETYPE2 support not available となってしまっている。

$  pip install pil -v
Downloading/unpacking pil
  Could not fetch URL http://www.pythonware.com/downloads/Imaging-1.1.3.tar.gz (from http://pypi.python.org/simple/PIL/): HTTP Error 404: Not Found
  Will skip URL http://www.pythonware.com/downloads/Imaging-1.1.3.tar.gz when looking for download links for pil
  Using version 1.1.7 (newest of versions: 1.1.7, 1.1.7a2-py2.5-macosx10.5.mpkg, wmf-1.0b2-20040224)
  Downloading PIL-1.1.7.tar.gz (506Kb): 506Kb downloaded
  Running setup.py egg_info for package pil
  中略
    --------------------------------------------------------------------
    PIL 1.1.7 SETUP SUMMARY
    --------------------------------------------------------------------
    version       1.1.7
    platform      darwin 2.7.2 (default, Nov  9 2011, 19:35:03)
                  [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)]
    --------------------------------------------------------------------
    --- TKINTER support available
    --- JPEG support available
    --- ZLIB (PNG/ZIP) support available
    *** FREETYPE2 support not available
    --- LITTLECMS support available
    --------------------------------------------------------------------
    To add a missing option, make sure you have the required
    library, and set the corresponding ROOT variable in the
    setup.py script.

この場合、blockdiagなどで、TrueTypeフォントを使おうとすると以下の様なエラーが出てしまう。

Exception occurred:
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/PIL/ImageFont.py", line 34, in __getattr__
    raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed

そこで、 No module named _imagingft を参考に以下の様にfreetype2の関連ライブラリのシンボリックリンクを張ってから PIL を再インストールすることで、 FREETYPE2 の support を available にできた。

$ pip uninstall PIL
$ ln -s /usr/X11/include/freetype2 /usr/local/include/
$ ln -s /usr/X11/include/ft2build.h /usr/local/include/
$ ln -s /usr/X11/lib/libfreetype.6.dylib /usr/local/lib/
$ ln -s /usr/X11/lib/libfreetype.6.dylib /usr/local/lib/libfreetype.dylib
$ pip install PIL
中略
    --------------------------------------------------------------------
    PIL 1.1.7 SETUP SUMMARY
    --------------------------------------------------------------------
    version       1.1.7
    platform      darwin 2.7.2 (default, Nov  9 2011, 19:35:03)
                  [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)]
    --------------------------------------------------------------------
    --- TKINTER support available
    --- JPEG support available
    --- ZLIB (PNG/ZIP) support available
    --- FREETYPE2 support available
    --- LITTLECMS support available
    --------------------------------------------------------------------
    To check the build, run the selftest.py script.
    changing mode of build/scripts-2.7/pilconvert.py from 644 to 755
    changing mode of build/scripts-2.7/pildriver.py from 644 to 755
    changing mode of build/scripts-2.7/pilfile.py from 644 to 755
    changing mode of build/scripts-2.7/pilfont.py from 644 to 755
    changing mode of build/scripts-2.7/pilprint.py from 644 to 755

    changing mode of /usr/local/share/python/pilconvert.py to 755
    changing mode of /usr/local/share/python/pildriver.py to 755
    changing mode of /usr/local/share/python/pilfile.py to 755
    changing mode of /usr/local/share/python/pilfont.py to 755
    changing mode of /usr/local/share/python/pilprint.py to 755
Successfully installed PIL

上手く行ったっぽい。

とりあえず現状の pip freeze

$ pip freeze
Jinja2==2.6
PIL==1.1.7
Pygments==1.4
Sphinx==1.1.2
actdiag==0.3.0
blockdiag==1.1.0
distribute==0.6.24
docutils==0.8.1
funcparserlib==0.3.5
git-remote-helpers==0.1.0
nwdiag==0.7.0
ordereddict==1.1
seqdiag==0.7.0
sphinxcontrib-actdiag==0.3.1
sphinxcontrib-blockdiag==1.0.1
sphinxcontrib-nwdiag==0.3.1
sphinxjp.themecore==0.1.3
sphinxjp.themes.s6==0.1.3
sphinxjp.themes.sphinxjp==0.1.1
webcolors==1.3.1
wsgiref==0.1.2