[python]venv環境でmatplotlibを使おうとするとRuntimeError: Python is not installed as a framework.エラーが発生するとき

カテゴリ: python | タグ: ,

pythonのvenv環境でmatplotlibを使おうとすると以下の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 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.
  • メッセージを意訳したもの
RuntimeError: Pythonがframeworkとしてインストールされていません
PythonがフレームワークとしてインストールされていないときMac OS Xのバックエンドは正しく動作することができません
MacOS X上でのPythonインストールについては、Pythonのドキュメントより、詳細な情報を確認してください。

Pythonをフレームワークとしてインストールし直すか、別のバックエンドを試してください
(Ana)Condaを使っている場合は、pythonを使っている箇所をpythonwに置き換えてください
Matplotlib FAQに記載されている'Working with Matplotlib on OSX'により詳細な情報があります。

行なったこと

今回は、以下の手順で環境を作ったらエラーが発生しました。

## venv環境を作って、有効化する
$ python -m venv env
$ source env/bin/activate

## pipでmatplotlibをインストール
(env)$ pip install matplotlib
Collecting matplotlib
  Downloading https://files.pythonhosted.org/packages/8a/d5/5337662b714c65100f3545ed3909e9478614d1ebf1f692a52981f3f5167b/matplotlib-2.2.2-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (13.7MB)
  ...
  Successfully installed cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.2 numpy-1.14.5 pyparsing-2.2.0 python-dateutil-2.7.3 pytz-2018.5 six-1.11.0

## インストールされたことを確認
$ pip list
cycler (0.10.0)
kiwisolver (1.0.1)
matplotlib (2.2.2)
numpy (1.14.5)
pip (9.0.3)
pyparsing (2.2.0)
python-dateutil (2.7.3)
pytz (2018.5)
six (1.11.0)
...
## matplotlibを使ったプログラムを実行
(env)$ python foo.py

...
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.

対応方法

今回は、エラーメッセージに表示されていたbackendを変更する方法で対処しました。
matplotlibのbackendは、設定ファイルで管理されており、下記のコマンドで確認できます。

import matplotlib
print(matplotlib.matplotlib_fname())

これを踏まえて、下記の手順で設定を変更しました。


## matplotlibのファイル名(フルパス)をbashの変数にセット $ filename_matplotlibrc=`python -c "import matplotlib;print(matplotlib.matplotlib_fname())"` ## 変数に入ったことを確認 $ echo $filename_matplotlibrc ..../lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc ## 現在のbackendの設定を確認 $ grep "^backend" $filename_matplotlibrc backend : macosx ## backendの設定をmacosxからTkaggに変更 $ sed -i -e "s/^backend.*: macosx$/backend : Tkagg/g" $filename_matplotlibrc ## 置換できたことを確認 $ grep "^backend" $filename_matplotlibrc backend : Tkagg ## 再度実行し、グラフが描画されることを確認 (env)$ python foo.py

Amazonでおトクに買い物する方法
AmazonチャージでポイントGET


Amazonは買いもの前にAmazonギフト券をチャージしてポイントをゲットしないと損!

こちらもおススメ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です