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
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”
LikeLike
Thanks for your feedback! You are right! It should be merely “python test.py”.
LikeLike