It is good to create an environment with runtime error => venv when using pyplot backends of macosx on a virtual environment created with virtualenv.

Introduction

When creating a virtual environment, it may be created with virtualenv. For example, it looks like the following. However, in the virtual environment created in this way, an error may occur when importing a module such as matplotlib.pyplot. I get an error when trying to use 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

Use -m venv instead of virtualenv

From around python3.3, the python standard library itself has a function to execute a virtual environment. Use this to create a virtual environment.

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

Other workaround

Play with PYTHON HOME.

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

reference

If it helps people around here

If you put a link. link This information should be transmitted to those who read the previous article.

-What to do if matplotlib.pyplot cannot be used when building an environment with pyenv and virtualenv --Qiita -Solution when you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3 --Qiita -Resolve Matplotlib Runtime Error --Qiita -Until you install and run matplotlib-- Qiita --Matplotlib etc. cannot be imported in VirtualEnv environment --Qiita -Memo I was addicted to when inserting matplotlib --Qiita

Recommended Posts

It is good to create an environment with runtime error => venv when using pyplot backends of macosx on a virtual environment created with virtualenv.
When I tried to create a virtual environment with Python, it didn't work
What to do if pip --user returns an error in a virtual environment created with pyenv
Steps to create a Python virtual environment with VS Code on Windows
How to create a Python virtual environment (venv)
Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS
[Venv] Create a python virtual environment on Ubuntu
What to do if you are told "Import Error: cannot import name'HTTPSHandler'" when building a virtual environment using virtualenv
When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.
A memo to create a virtual environment (venv) before Django
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
Overview of Python virtual environment and how to create it
Create an environment with virtualenv
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
When you want to use multiple versions of the same Python library (virtual environment using venv)
How to quickly create a machine learning environment using Jupyter Notebook on macOS Sierra with anaconda
I tried to create an environment of MkDocs on Amazon Linux
If you upgrade the kernel version when the host OS is Linux, an error occurred when launching the virtual environment of VirtualBox, so a memorandum
PATH when using ANACONDA virtual environment with Pycharm (as of Mac 2020/10/03)
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
A story when a directory is buggy with the django-admin start project command in a virtual environment using Pipenv
Create a virtual environment with Python!
When you want to use it as it is when using it with lambda memo
It is convenient to use Layers when putting a library on Lambda
What to do if an error occurs when loading a python project created with poetry into VS Code
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
[Python] Create a virtual environment with Anaconda
pyenv-Building a pypy environment using virtualenv (MacOSX)
Create a virtual environment with Python_Mac version
It is more convenient to use csv-table when writing a table with python-sphinx
How to build an environment for using multiple versions of Python on Mac
Creating an environment for OSS-DB Silver # 1_Create a Linux environment (CentOS7 virtual environment) with VirtualBox/Vagrant
I tried to create an environment to check regularly using Selenium with AWS Fargate
Countermeasures for "Unable to get upper directory" error when using Deep Learning ② created from scratch with spyder of ANACONDA
Is it possible to create an infinite length "non-repeating character string" with only three types of characters? [BFS] [DFS]