Solve the error of running Matplotlib on Tensorflow on Mac OSX

If you are running Matplotlib on Mac to draw some figures, you might in counter some problem such as:

“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”

The solution can be found here:

https://matplotlib.org/faq/osx_framework.html

In my case, I am using Python provided in Anaconda. Every is all right when I am just running Matplotlib to plot some figures of my python files. I simply use pythonw instead of python to run python files.

Below is the command I used to run a “test.py” file on terminal.

$ pythonw test.py

However, when I am running Matplotlib of some python files on tensorflow, pythonw has no module called tensorflow. Since using python could successfully run tensorflow, using pythonw would fail. The solution that I found is to write some codes in the matplotlibrc file under the path “/.matplotlib/matplotlibrc”.

$ vim ~/.matplotlib/matplotlibrc

Inside this file, write code

$ backend: TkAgg

That’s it! Save the changed and run again the python file again, and this time you use python instead of pythonw.

$ python test.py

Cheers!

Ting-Hao Chen
Aug 24, 2017

Advertisements

2 thoughts on “Solve the error of running Matplotlib on Tensorflow on Mac OSX

  1. yhyu13

    Thanks! It solves my problem. Just a reminder to a minor glitch, your last line of code must be “python test.py” instead of “pythonw test.py”

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s