matplotlibをimportしたら"Python is not installed as a framework."というエラーが出た


スポンサーリンク

matplotlibをimportしたら以下のようなエラーが出ました。

>>> from pylab import plot, show
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/UserName/.local/share/virtualenvs/chap2-U29tY3uW/lib/python3.6/site-packages/pylab.py", line 1, in <module>
    from matplotlib.pylab import *
  File "/Users/UserName/.local/share/virtualenvs/chap2-U29tY3uW/lib/python3.6/site-packages/matplotlib/pylab.py", line 252, in <module>
    from matplotlib import cbook, mlab, pyplot as plt
  File "/Users/UserName/.local/share/virtualenvs/chap2-U29tY3uW/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/UserName/.local/share/virtualenvs/chap2-U29tY3uW/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
    [backend_name], 0)
  File "/Users/UserName/.local/share/virtualenvs/chap2-U29tY3uW/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 17, in <module>
    from matplotlib.backends import _macosx
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.
>>> 

これを解決するには、

vim ~/.matplotlib/matplotlibrc

matplotlibrcというファイルを作り、ファイルの中にbackend: TkAggと書けばOKです。

https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python