What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3

Background

So I decided to do machine learning using Scikit-learn. I also decided to use matplotlib because I wanted to visualize the figure. When I imported pylab to use matplotlib easily, I got the error RuntimeError: Python is not installed as a framework, so I changed the matplotlib settings and solved the problem.

Installation procedure

$ pip install scikit-learn
$ brew install freetype
$ sudo pip install python-dateutil
$ sudo pip install pyparsing
$ sudo pip install matplotlib

To install matplotlib

Is necessary. There are many other things you need. For more information, please visit the Official Page

If you brew install freetype, the dependent libpng will also be included.

Reference http://djakarta-trap.net/blog/2013/06/07/install_matplotlib/

a problem occured

In a dialogue environment

ipython3


import pylab

Then

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.

Error occurred.

Investigation

First, I checked if there were any people who were in trouble with the same error and who solved it.

Discussion on a project using matplotlib called yt http://hg.yt-project.org/yt/issue/642/cannot-import-pylab-on-os-x-using

Japanese blog article that ran into the same problem http://asahima.hatenablog.com/entry/2013/10/11/201119

matplotlib issues on the original github https://github.com/matplotlib/matplotlib/issues/2361

Apparently it hasn't been resolved yet as of October 2013.

Cause

The matplotlib image drawing backend (reference http://matplotlib.org/faq/usage_faq.html#what-is-a-backend) is "macosx" which renders using Cocoa's API by default. GTK Agg and Qt4Agg are non-default backends. When using from Linux or Windows, of course, set a backend other than macosx.

My reasoning

The problem is that in my environment I used pyenv to install Python 3.3 in a different location than it was originally on my OSX system. In this case, it may not be possible to draw with Cocoa's API, so when importing pylab, an error will occur just in case.

Specifying the backend to something other than macosx solves the problem.

Solution

Since you installed matplotlib with pip, you should have a directory called ~ / .matplotlib. Create a file called matplotlibrc there.

~/.matplotlib/matplotlibrc


backend : TkAgg

And specify to use TkAgg as the backend.

How to write matplotlibrc Reference http://matplotlib.org/users/customizing.html

Experiment

I ran the code that visualizes the result of sklearn in pylab, which is introduced in this blog article http://sucrose.hatenablog.com/entry/2013/05/25/133021, in an interactive environment.

from sklearn.datasets import load_digits
import pylab as pl 
digits = load_digits()
pl.gray() 
pl.matshow(digits.images[0]) 
pl.show() 

figure_2.png

In this way, I was able to draw a beautiful figure even with TkAgg.

Recommended Posts

What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
What to do if you get a must override `get_config` error when trying to model.save in Keras
[Mac OS] What to do when Python is not installed as a framework. Is displayed when import matplotlib is performed.
What to do if you get an error when trying to send a message in tasks.loop () immediately after startup
What to do if you get a minus zero in Python
What to do if you get Swagger-codegen in python and Import Error: No module named
What to do if you get an Undefined error when trying to use pip with pyenv
What to do if you get an error when trying to load mnist
What to do if you get a "No versions found" error in pipenv
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
What to do if you get an error when installing python with pyenv
What to do if you get a Permission denied (public key) error when trying to pull on Github
What to do if you get "Python not configured." Using PyDev in Eclipse
[Python] What to check when you get a Unicode Decode Error in Django
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do if you get a memory error when converting from PySparkDataFrame to PandasDataFrame
What to do if the progress bar is not displayed in tqdm of python
What to do if you get the error ╩╗ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
What to do if you run python in IntelliJ and end with an error
Solution if the module is installed in Python but you get an error in Jupyter notebook
What to do if you get a Cannot retrieve metalink for repository error in yum
What to do if you get an error when running "certbot renew" in CakePHP environment
What to do if you can't use the trash in Lubuntu 18.04.
What to do when the value type is ambiguous in Python?
What to do if you cat or tail a binary file and the terminal is garbled
NameError: global name'dot_parser' is not defined and what to do when it comes up in python
What to do if there is a decimal in python json .dumps
What to do if you can't use scikit grid search in Python
What happens if you do "import A, B as C" in Python?
What to do if you get an error when installing Dlib (Ubuntu)
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
What to do if you get angry with "Value Error: unknown local: UTF-8" in python manage.py syncdb
What to do if a symbolic link error occurs in import cv while trying to install OpenCV in Python
What to do if you get the message "" ~ .pkg "is corrupted and cannot be opened" when installing wxPython on Mac OS X
What to do if a version error occurs in the selenium Chrome driver
What to do if you get the error Input contains NaN, infinity or a value too large for dtype ('float64'). In machine learning
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
What to do when is not in the sudoers file.This incident will be reported.
What to do if you get `No kernel for language python found` in Hydrogen
What to do if the print command itself causes an error in Maya python
What to do if you get "(35,'SSL connect error')" in pycurl (one of them)
If you get the error "basis matrix is singular to working precision" in GLPK
What to do if Python IntelliSense is not displayed in VS Code on Windows
How to install and use pyenv, what to do if you can't switch python versions
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if Python does not switch from the System version in pyenv
What to do if you can't hit the arrow keys in the Python interactive console
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
What to do if you get a Call with too many input arguments error at DoAndReturn in a golang test
In pandas.DataFrame, even when assigning only a specific column, if index is attached, you do not have to worry about the order of data
Install the python library when you're not a sudoer (or if you don't want new users to be Sudoer, but you don't mind having the Python Library installed as you like)
What to do if you get "coverage unknown" in Coveralls
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
What to do if you can't log in as root
What to do if the image is not displayed using matplotlib etc. in the Docker container
What to do when a Missing artifact occurs in a jar that is not defined in pom.xml
[Python] When you want to import and use your own package in the upper directory