virtualenv에 matplotlib 설치하기(MAC OS)


1단계: virtualenv로 가서 pip 명령을 이용해 설치한다.

$ source [설치한 경로]/bin/activate

(설치폴더이름) $ pip3 install matplotlib

이렇게 하면 알아서 depentent library도 설치해가며 완료가 된다.



2단계: import matplotlib as plt 해도 에러가 난다.

Traceback (most recent call last):

  File "/Users/[user]/workspace/learning_TensorFlow/cpu_python3/learning_lab3_1.py", line 3, in <module>

    import matplotlib.pyplot as plt

  File "/Users/[user]/tf_py_3/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/[user]/tf_py_3/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup

    globals(),locals(),[backend_name],0)

  File "/Users/[user]/tf_py_3/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 19, 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.

설치를 했는데 이건 또 뭔가...

이건 https://github.com/dsmbgu8/image_annotate.py/issues/4 에 잘 나와있다.

3단계에서 해결해보자.



3단계: matplotlib의 backend를 변경해준다.

1) 스크립트 상에서

import matplotlib

matplotlib.use('TkAgg')

import matplotlib.pyplot as plt


2) virtualenv bash 상에서

(설치폴더이름) $ echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc



4단계: matplotlib의 backend가 뭐길래...

- Graph등을 그릴때 Python을 실행하는 환경에 대한 지원을 해주는 부분이고 이것을 입맛대로 골라서 쓰면된다.

  설명대로라면 어떤이는 콘솔에서 띄우고 싶고, 어떤이는 툴에서 띄우고 싶고, 어떤이는 서버에서 띄우고...

  이런 다양한 환경을 지원하는 것이 "backend" 라고 한단다.

- 다양한 backend는 https://matplotlib.org/faq/usage_faq.html 에서 확인할 수 있다.



Reference

- https://github.com/dsmbgu8/image_annotate.py/issues/4

- https://stackoverflow.com/questions/29433824/unable-to-import-matplotlib-pyplot-as-plt-in-virtualenv

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

+ Recent posts