by shigemk2

当面は技術的なことしか書かない

matplotlibをpyenvで使おうとしたときのRuntimeError

  • Python 2.7.11(pyenv)
  • matplotlib 2.2.2
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

plt.plot(x, y)
plt.show()

無邪気にpyenvでmatplotlibを動かそうとしたら、以下のようなエラーに見舞われた。

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 using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

ググったら同じトラブルに巻き込まれた人をQiitaでいっぱい見たけど、解決方法しか書いていなくて出典もSOだったから原因がどうなっているのかよくわからなかった。

エラーログをよく読むと、 See 'Working with Matplotlib on OSX' in the Matplotlib FAQ って書いてあるから、こっちを調べてみた。

https://matplotlib.org/faq/osx_framework.html

https://matplotlib.org/faq/usage_faq.html

以下わかったこと。

  • OSXでmatplotlib使おうとする場合フレームワークビルドが使えるかどうかをチェックしている
  • OSXの場合pyenv (Ana)Conda virtualenvを使おうとすると、フレームワークとしてインストールされていないと描画が失敗する
  • エラーログにも書いてあるけど、↑のリンクのFAQに沿ってPythonを入れ直すか、pythonwを使うようにすること