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.

3
  • I think your question is answered at stackoverflow.com/questions/4130355/… Commented Jul 12, 2015 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. Commented Jul 13, 2015 at 1:29
  • Possible duplicate of python matplotlib framework under macosx? Commented Apr 5, 2017 at 7:24

10 Answers 10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

this worked for me if I was using a conda environment but not if I was using a virtualenv environment.
At least for me, this also worked when matplotlib was installed with conda and not just pip.
Why doesn't matplotlib do this automatically?
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.
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.

Comments

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.

1 Comment

Actually, if you want to use this backend by default, follow the instruction of the main answer of stackoverflow.com/questions/21784641/…
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

3 Comments

Glen, installing an older version of the library may have solved the issue for you, but the conflicting PATH set by mac ports/Homebrew is what was causing the error for me. I have since put a little more effort into envs
Works on OSX 10.11 El Capitan too
Works on Catalina
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.

Comments

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

Comments

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

1 Comment

For Anaconda the .bash_profile should have the following: export PATH="/Users/username/anaconda/bin:$PATH"
0

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

1 Comment

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
0

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

conda install matplotlib

1 Comment

In this case, that’s not the solution. The conflicting path declarations from MacPorts and homebrew we’re responsible.
0

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

e.g pythonw testFile.py.

2 Comments

Can you provide an explanation of what the difference is?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.