LoginSignup
15
13

More than 5 years have passed since last update.

VirtualEnv環境でmatplotlibなどがimportできない

Last updated at Posted at 2015-12-11

環境

  • Python 3.4.3(Pyenvにて)
  • ipython==4.0.1
  • matplotlib==1.5.0
  • notebook==4.0.6

2015年12月11日現在最新版がはいってるということ

エラー内容

Traceback (most recent call last):
File "", line 1, in
File "/Users/user/graph/lib/python3.4/site-packages/matplotlib/init.py", line 1131, in
rcParams = rc_params()
File "/Users/user/graph/lib/python3.4/site-packages/matplotlib/init.py", line 975, in rc_params
return rc_params_from_file(fname, fail_on_error)
File "/Users/user/graph/lib/python3.4/site-packages/matplotlib/init.py", line 1100, in rc_params_from_file
config_from_file = rc_params_in_file(fname, fail_on_error)
File "/Users/user/graph/lib/python3.4/site-packages/matplotlib/
init.py", line 1018, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "/Users/user/.pyenv/versions/3.4.3/lib/python3.4/contextlib.py", line 59, in __enter
_
return next(self.gen)
File "/Users/user/graph/lib/python3.4/site-packages/matplotlib/init.py", line 1000, in _open_file_or_url
encoding = locale.getdefaultlocale()[1]
File "/Users/user/graph/lib/python3.4/locale.py", line 556, in getdefaultlocale
return _parse_localename(localename)
File "/Users/user/graph/lib/python3.4/locale.py", line 484, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: UTF-8

要はlocale: UTF-8

locale設定

export

(graph)pc-name:graph user$ export LC_ALL='ja_JP.UTF-8'

確認

(graph)pc-name:graph user$ locale
LANG=
LC_COLLATE="ja_JP.UTF-8"
LC_CTYPE="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME="ja_JP.UTF-8"
LC_ALL="ja_JP.UTF-8"     # こいつ

再度importできるか

>>> import matplotlib.pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/.pyenv/versions/3.4.3/lib/python3.4/site-packages/matplotlib/pyplot.py", line 114, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/user/.pyenv/versions/3.4.3/lib/python3.4/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Users/user/.pyenv/versions/3.4.3/lib/python3.4/site-packages/matplotlib/backends/backend_macosx.py", line 24, 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 Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ

まだなんかでる...

~/.matplotlib/matplotlibrcを作成

backend : TkAgg

を追加

別解

また、 @aion さんからご指摘をいただき以下でもこのエラーへの対策ができるそうです!
@aion さんありがとうございます:muscle:

import matplotlib
matplotlib.use('TkAgg')
15
13
2

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
15
13