matplotlibをOSXのpyenv仮想環境下で使用するには。

matplotlibを使いたかっただけなのに、まさかトラブるとは思っていませんでした…。
ちょと前まで普通に使えていたはずなのですが、そういえばSSD換装後にOSXを再インストールして以降、使った覚えがない。
pyenv環境下でmatplotlibを使用するためには色々と下準備が必要ということを今更知り、とりあえず解決したので、その備忘録です。

import matplotlibでいきなり警告

pip install matplotlibは何事もなく成功するのですが、import matplotlibでおもむろに警告が出力されます。
が、これは問題ありません。
フォントのキャッシュを作ってるのでしばし待てと。
OSX再インストール前にも表示されたかどうかは… うーん、記憶にない。

In [11]: import matplotlib.pyplot as plt
/Users/takashi/.anyenv/envs/pyenv/versions/3.5.1/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/Users/takashi/.anyenv/envs/pyenv/versions/3.5.1/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

そしてRuntimeError

フォントキャッシュ作成を待つこと数分の後、出ました、エラー。

RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

仮想環境下でmatplotlib使うには、どうも何か下準備が必要そうです。
Matplotlib FAQを見ろとあったので、ググってみたところ、ありました。

matplotlibの問題ではないと前置きした上で(そこ強調するよね)、GUIフレームワークとのインタラクションのためにおまじないが必要なのだそう。
OSXの場合は、以下のリンクを参照してくださいとのこと。
しっかりpyenv環境下での対処法が記載されています。

PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install x.x.x

RuntimeError解決方法

--enable-frameworkオプションを有効にした上でインストールせよとのこと。
やってみます。

moca:python $ PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.5.1
Downloading Python-3.5.1.tgz...
-> https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
Installing Python-3.5.1...
Installed Python-3.5.1 to /Users/takashi/.anyenv/envs/pyenv/versions/3.5.1
moca:python $ pyenv global 3.5.1
moca:python $ pip install ipython matplotlib
Collecting ipython
  Downloading ipython-5.2.2-py3-none-any.whl (749kB)
    100% |________________________________| 749kB 377kB/s
...

インストールは普通に完了したので、必要なパッケージをインストールします。

moca:python $ ipython
Python 3.5.1 (default, Feb  7 2017, 06:25:06)
Type "copyright", "credits" or "license" for more information.
IPython 5.2.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
In [1]: import matplotlib.pyplot as plt
In [6]: png = plt.imread("/Users/takashi/Downloads/auth_ssl.png")
In [7]: plt.imshow(png)
Out[7]: <matplotlib.image.AxesImage at 0x10a825588>

エラーなく動きました… が、imshowした後にウィンドウが表示されるはずが、何も表示されない…。
ipython notebookでブラウザ表示できればいいか…。

まとめ

とりあえず、RuntimeErrorなしにpyenv環境下でmatplotlibが使えるようになりました。
plt.imsave()すれば、バッファ上に確保した画像データをpngファイルに吐き出すこともできたので、当面は問題なさそうです。

In [7]: plt.imsave("/Users/takashi/temp.png", png)
In [8]:
Do you really want to exit ([y]/n)? y
moca:python $ open /Users/takashi/temp.png

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

Close Bitnami banner
Bitnami