How to fix Python was not installed as a framework on Mac OSX with Matplotlib in virtualenv

Do you use Mac OS as your development environment? Do you use python for your day to day work? Do you do machine learning or data presentation and Matplotlib is in your tool belt? Any of us worked with Matplotlib in a virtualenv in Mac OS, probably faced this following error:

**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.

I did too.

The fix is very easy, and only consists of 1 line of code.

import matplotlib as mpl
mpl.use('TkAgg')
The reason for this problem is matplotlib separates the renderer and the canvas from each other. The default renderer uses AGG (Anti-Grain Geometry) C++ library to generate the graph's raster images. On Mac OS, we need to use the TK backend for the canvas. So, in the second line of code above, we are just telling matplotlib to use the TkAgg backend for the canvas to draw images.

Voila! The problem is solved. After this you can do the import for pyplot without any problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top