LoginSignup
1
0

More than 5 years have passed since last update.

virtualenvで作った仮想環境上でpyplotをmacosxのbackendsを使った時にruntime error => venvで環境作成すると良い

Last updated at Posted at 2016-09-17

はじめに

仮想環境の作成の際にvirtualenvで作る場合がある。例えば以下の様な感じ。
しかしこのようにして作った仮想環境では matplotlib.pyplot 等のmoduleをimportしたときにエラーが出てしまう場合がある。macosxのbackendを使おうとするとエラーになる。

$ virtualenv --python=`which python3.5` --system-site-packages viz
$ . ./viz/bin/activate
(viz)$ python -c 'import matplotlib.pyplot'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/pyplot.py", line 114, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/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

virtualenvではなく-m venv を使う

python3.3あたりからpythonの標準ライブラリ自体が仮想環境を実行するための機能を持つようになった。こちらを使って仮想環境を作ると良い。

$ python -m venv --system-site-packages viz2
$ . ./viz2/bin/activate
(viz2)$ python -c 'import matplotlib.pyplot'

他のworkaround

PYTHONHOMEを弄る。

(viz)$ deactivate
$ which python
/opt/local/bin/python
$ . viz/bin/activate
(viz)$ PYTHONHOME=$VIRTUAL_ENV /opt/local/bin/python -c 'import matplotlib.pyplot'

参考

この辺の人の参考になれば

linkを貼れば。link先の記事を見た人にもこの情報が伝わるはず。

1
0
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
1
0