LoginSignup
0
2

More than 5 years have passed since last update.

【Python】mac で matplotlib 使用時に ImportError: Python is not installed as a framework. が出る場合の対処法

Last updated at Posted at 2019-01-26

事象

macOS で matplotlib を使用したプログラムを実行したところ次のようなエラーが発生しました。

$ python main.py
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    import matplotlib.pyplot as plt
  File "/Users/RM/Documents/deep-learning-practice/.venv/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2374, in <module>
    switch_backend(rcParams["backend"])
  File "/Users/RM/Documents/deep-learning-practice/.venv/lib/python3.7/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/Users/RM/Documents/deep-learning-practice/.venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/RM/Documents/deep-learning-practice/.venv/lib/python3.7/site-packages/matplotlib/backends/backend_macosx.py", line 14, in <module>
    from matplotlib.backends import _macosx
ImportError: 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.

対処法

ターミナルで次のコマンドを実行します。

sed -i -e 's/backend: TkAgg/backend: Agg/' $(python -c "import matplotlib;print(matplotlib.matplotlib_fname())")

参照

こちらの記事で解決しました。
Pythonでmatplotlibをimportするとエラーが出る場合の対処策(Mac)

補足

python -c <<コマンド>> はコマンドラインで Python を実行します。

$() はコマンド内でコマンドを実行します。

sed -e "s/置換前/置換後/" 対象ファイル は対象ファイルの対象文字列を置換して文字列を返却します。

sed -i で破壊的にコマンドを実行します。要するに対象を直接編集します。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2