39

I've installed Anaconda with the pkg installer:

Python 2.7.10 |Continuum Analytics, Inc.| (default, May 28 2015, 17:04:42) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org

but when I attempt to use anything from matplotlib, i.e.:

 from matplotlib import pyplot as plt

I get

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'm really not sure what this means, or how to go about fixing it.

Mark Brown
  • 914
  • 1
  • 11
  • 26
  • I think your question is answered at http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx –  Jul 12 '15 at 23:41
  • It took going down a bit of a rabbit hole with that post, but it brought me to conflicts generated by using Homebrew, Macports, and others. I had to edit my bash_profile -- having previously used Homebrew and MacPorts, I had $PATH declarations that were conflicting. After removing those entries, matplot lib works fine. I'm sure I broke something somewhere in some old code I have, but Anaconda seems to be the implementation of Python I've really been after for some time. – Mark Brown Jul 13 '15 at 01:29
  • Possible duplicate of [python matplotlib framework under macosx?](http://stackoverflow.com/questions/4130355/python-matplotlib-framework-under-macosx) – tripleee Apr 05 '17 at 07:24

10 Answers10

60

Posting since I just had this issue and this was a quick fix:

If you used pip to install:

  1. Create ~/.matplotlib/matplotlibrc

  2. Add "backend: TkAgg" (without the quotations) to the file.

Jared Wilber
  • 6,038
  • 1
  • 32
  • 35
  • this worked for me if I was using a conda environment but not if I was using a virtualenv environment. – user3391229 Dec 27 '18 at 20:46
  • At least for me, this also worked when matplotlib was installed with conda and not just pip. – F Lekschas Feb 10 '19 at 18:58
  • Why doesn't `matplotlib` do this automatically? – Janosh Mar 20 '19 at 11:55
  • Just for posterity can someone please explain what changing the "backend" does as well as what it means for Python to be installed "as a framework?" Oddly, I couldn't find a succinct answer to this on the web anywhere. – John Carrell Apr 11 '19 at 13:18
35

I was having the same problem with anaconda 2 & matplotlib 1.5.3.

Running a simple conda install matplotlib to reinstall matplotlib did the trick for me.

Daniel
  • 11,332
  • 9
  • 44
  • 72
19

If the problem is only matplotlib, is worth try to change the backend:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

If it works you can change the backend permanently from the matplotlibrc file.

SeF
  • 3,864
  • 2
  • 28
  • 41
  • 1
    Actually, if you want to use this backend by default, follow the instruction of the main answer of http://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python – SeF Feb 16 '17 at 14:57
9

I was having the same problem. Installing an older version of matplotlib did the trick for me. Try this command in your terminal while in your virtual environment:

pip install matplotlib==1.4.3
Glenn Cameron
  • 109
  • 1
  • 5
7

Run the file using pythonw instead of python. This happens because python is not installed as a framework. Therefore use pythonw myScript.py instead of python myScript.py I am sure this will fix it.

I had a similar 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. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

devssh
  • 1,184
  • 12
  • 28
2

From the matplotlib documentation;

$ conda install python.app

You need a framwork build of Python for matplotlib, but

The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python

NB I had to add the conda-forge channel as python.app isn't included in the default miniconda channels

$ conda config --add channels conda-forge

danodonovan
  • 19,636
  • 10
  • 70
  • 78
0

If you experience this error, don't forget to check your bash_profile.

You can do this in terminal by:

cd

then

nano .bash_profile

check the contents. Macports and Homebrew add their own headings for things they've done here. You can remove the declarations they make to $PATH. Just leave the one Anaconda has made. I had a If you would like, you can:

cp .bash_profile ./bash_profile_backup_yyyy_mm_dd 

and have a backup of the file, with filename indexing to the date you changed it. That is, provided you actually put in the date in instead of just the formatting characters I'm suggesting.

source ~/.bash_profile

will refresh your system's reference to the bash_profile and you should be good to go in importing and using matplotlib

Mark Brown
  • 914
  • 1
  • 11
  • 26
0

if using inside a virtualenv, I recommend following the instructions here: http://matplotlib.org/faq/virtualenv_faq.html

Bobby
  • 6,840
  • 1
  • 22
  • 25
  • Good instructions. Even if inside a virtualenv, conflicts from other installations may still exist. Be sure to check the .bash_profile for $PATH declarations if you have the troubles I mentioned – Mark Brown Nov 10 '15 at 21:46
0

A reinstall of matplotlib should fix the issue for you as it did for me with

conda install matplotlib

  • In this case, that’s not the solution. The conflicting path declarations from MacPorts and homebrew we’re responsible. – Mark Brown Mar 10 '18 at 21:55
0

Quickfix: Run your file using pythonw, instead of python.

e.g pythonw testFile.py.

Sehul Viras
  • 587
  • 5
  • 9
  • Can you provide an explanation of what the difference is? – jhpratt Apr 04 '18 at 00:19
  • Note: this post had macos in the tags. I presume running under a different env is the solution running the script with pythonw achieves, though isn't an answer that promotes python literacy. – Mark Brown Apr 08 '18 at 04:18