たまめ的生活記録

日々で思ったこととか、技術的なこととか。

Pythonのmatplotlibをimportしようとするとエラーが出た。その対処法。

課題で数値をグラフにプロットしたかったのでPythonのmatplotlib.pyplotを使おうとしたらエラーが出た。僕はQiitaで役立った記事とかをまとめたりするような習慣がないので(いいのかそれでという気もするけど)、備忘録としてここに書いておきます。

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.

バックエンドのエラーらしい。なので設定を直接書き換える。
設定ファイルは下記コマンドで見つかるみたいです。

$ python -c "import matplotlib;print(matplotlib.matplotlib_fname())"

出てきたディレクトリに移動して、matplotlibrcをテキストエディタで開く。

backend : macosx

と書いてある部分があるので、次のように書き換えます。

backend : Tkagg

これで無事importできるようになりました。

環境

参考

qiita.com