23

I am aware that this exact same question has been asked before. I did follow the instructions given in the answer there, and it didn't solve my problem (and I don't have enough reputation to just comment on the Q or A in that thread). Anyway, here's what's going on:

I try to do:

import matplotlib.pyplot

And in return I get:

Traceback (most recent call last):
  File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3032, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-eff513f636fd>", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in <module>
    import matplotlib.colorbar
  File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in <module>
    import matplotlib.collections as collections
  File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in <module>
    import matplotlib.backend_bases as backend_bases
  File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in <module>
    import matplotlib.textpath as textpath
  File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 22, in <module>
    from matplotlib.mathtext import MathTextParser
  File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.py", line 63, in <module>
    import matplotlib._png as _png
ImportError: dlopen(/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/_png.so, 2): Library not loaded: libpng15.15.dylib
  Referenced from: /Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/_png.so
  Reason: image not found

My Python version:

2.7.7 |Anaconda 2.0.1 (x86_64)| (default, Jun  2 2014, 12:48:16) [GCC 4.0.1 (Apple Inc. build 5493)]

EDIT:

cel's suggestion worked! I just tried "conda remove matplotlib", "pip install matplotlib", and then "conda install matplotlib", and presto! Man, you have no idea how long this problem has vexed me. Bless you all.

Community
  • 1
  • 1
Russell Richie
  • 421
  • 1
  • 5
  • 15
  • 8
    How did you install `matplotlib`? Try uninstalling with `conda remove matplotlib` and `pip uninstall matplotlib` and then see if `conda install matplotlib` solves this issue. – cel Mar 04 '15 at 07:16
  • 1
    **upvoted**, and hopefully others will so you can get the rep to ask/comment in the other mentioned thread. – l'L'l Mar 04 '15 at 07:21
  • @Russell Richie, I think this is a matter of not having the `libpng15.15.dylib` installed. which you must not? I believe it's part of `php55+` – l'L'l Mar 04 '15 at 07:24
  • 3
    Removing and reinstalling matplotlib did not work until I reinstalled libpng first (`brew unlink libpng` then `brew install libpng`). I then `conda remove matplotlib` and `conda install matplotlib` and `%matplotlib inline` no longer generates any error. – Alexis Perrier Jun 09 '15 at 16:46
  • @cel I realize that this is an old post, but you should probably post your comment that solved the OP's problem as an answer, so that the question no longer appears in the unanswered queue. If Russell is still around, he could also select it as the answer. Win/Win – AMR Aug 08 '15 at 04:35
  • 1
    @AlexPerrier Homebrew has a much different way of installing python packages they follow [PEP 0453](https://www.python.org/dev/peps/pep-0453/) guideline that PIP be the default package installer for all Python Implementations, including those from Downstream Distributors make PIP available as the default package manager for Python. – AMR Aug 08 '15 at 04:46
  • 1
    @AMR, Thanks for the notification. I decided to put together some information about this issue in an answer. When I commented I did not assume that this question would become so popular. – cel Aug 08 '15 at 11:04

3 Answers3

21

Some python packages link dynamically against native c libraries. After an update of one of those libraries, links can break and give you weird error messages about missing dynamic libraries, as seen in the error message in the question.

Basically, after an update of a native library sometimes you also have to rebuild python packages (here matplotlib).

The above statement is true in general. If you are using conda as your python distribution things are usually less complicated:

For extension packages conda also maintains required c libraries. As long as you use only conda install and conda update for installing those packages you should not run into these issues.

For numpy, scipy, matplotlib and many more I would suggest to try conda search <library name> first to see if there's a conda recipe that matches your needs. For most users conda install <library name> will be a better option than pip install.

To make sure that only conda's version is installed you can do

conda remove matplotlib
pip uninstall matplotlib
conda install matplotlib

Afterwards this issue should not appear anymore.

cel
  • 30,017
  • 18
  • 97
  • 117
2

I ran into this problem as well. I updated my Anaconda-Navigator and the next time I opened a project with matplotlib.pyplot, I ran into a similar problem. What worked for me was:

conda install libpng
ricopella
  • 63
  • 7
1

I had this problem, but it was because I had set

export DYLD_LIBRARY_PATH="/Users/charlesmartin14/anaconda/lib":$DYLD_LIBRARY_PATH

removing this setting and restarting the shell fixed it

Charles
  • 495
  • 1
  • 5
  • 12