Data Science by R and Python

統計学を、広く、深く、わかりやすく。

メモ@Python - sys.pathの問題「Extra/lib/python」という罠

夏休み以来、一度も投稿していませんでした。完全に、ブログ書くのも忘れて、研究にいそしんでおります...最近、Rからpythonに色々と移行しつつありまして、環境設定で、原因不明のエラーに苦しめられております。そろそろ、またいろんなlibraryを開拓したのでちょこちょこそういうのも載せていこうと思います。

python sys.path問題

ここ数日、pythonでnumpy, pandasなどをアップデートしたのに、アップデートしたものが、どういうわけだかimportすると昔のバージョンが使用される問題がありまして、かなり困っておりました。どうも、sys.pathに含まれている、以下のpathが悪さをしていることが分かりました。

sys.pathの中身

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Python/2.7/site-packages

悪さしてるpath

'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python'

なんだろこのpath...って思って、色々調べてみたのですが、こんな記事が出てきました...
Which Python · MacPython/wiki Wiki · GitHub

タイトルをみれば分かる通り...

「Why you should not use system Python for OSX

--OS XPythonシステムを使うべきではない理由

ということです。

で、その中にこんなことが書かれていました。
OSX 10.9のシステムでは、numpy, scipy, matplotlibや、ウェブ系で便利なパッケージが入っています。そして、そのpathとして、"/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"というものを採用しているようです。

その結果、新しいパッケージをインストールすると、
"/Library/Python/2.7/site-packages"
に本来ならインストールされて、pythonのpath内にこれは含まれているので、参照されるわけですが、このpathが"/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python"のpathよりも「下位」のものとしてリストに含まれています。なので、まずは/Extras/lib/pythonを先に参照してから、/site-packagesを読むため、2つのサイトに「numpy」が含まれていれば、先に読まれる方が優先されます。

そして、pip install numpyでインストールすると「site-packages」に入ってしまうため、いくらpipで最新版をインストールしても"/Extra"に入っているnumpy1.6.1が参照されます。などということで、標準搭載のpythonは非常に使いにくいということでした。原文は以下。

The interesting entry is "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python" because this entry is specific to system Python. Call this path "Extras/lib". On a standard OSX 10.9 machine this directory contains some Python packages including the numerical packages numpy, scipy and matplotlib and other useful web packages including twisted.

If you, the user, install new packages in system Python, the packages will get written to /Library/Python/2.7/site-packages. As you can see from the path listing above, your newly installed package will therefore be lower in the Python module path than the packages in Extras/lib. That means that if you try to install a new version of one of the packages in Extras/lib into system Python, system Python will ignore the version you installed and continue to use the version in Extras/lib.

Apple presumably chose this setup because they use Python for system tasks, and they want to be able to depend on working versions of the packages installed in Extras/lib. This is another reason to prefer another Python for your daily Python work.

まとめ

ということだそうなので、標準搭載のpythonを捨てて、macの皆様は是非homebrewでpythonをぶちこむか、以下のリンクなどを使って、構築した方がいいのかもしれません。
pyenvでPython開発環境を構築 | youria blog

ちなみに、僕はhomebrewでpythonを入れるのをまだしていなくて、標準搭載のpythonの'Extra/lib'のフォルダーを削除してやりました笑。←ダメだと思いつつ笑

ということで、これぐらいにします。