import matplotlib.pyplot gives ImportError: dlopen(...) Library not loaded libpng15.15.dylib

travelingbones picture travelingbones · Dec 3, 2014 · Viewed 10k times · Source

Importing pyplot gives an error:

In [1]: import matplotlib
In [2]: import matplotlib.pyplot as plt
ImportError                               Traceback (most recent call last)
<ipython-input-2-eff513f636fd> in <module>()
----> 1 import matplotlib.pyplot as plt
/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
     25
     26 import matplotlib
---> 27 import matplotlib.colorbar
     28 from matplotlib import style
     29 from matplotlib import _pylab_helpers, interactive

/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py in <module>()
     32 import matplotlib.artist as martist
     33 import matplotlib.cbook as cbook
---> 34 import matplotlib.collections as collections
     35 import matplotlib.colors as colors
     36 import matplotlib.contour as contour
/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/collections.py in <module>()
     25 import matplotlib.artist as artist
     26 from matplotlib.artist import allow_rasterization
---> 27 import matplotlib.backend_bases as backend_bases
     28 import matplotlib.path as mpath
     29 from matplotlib import _path
/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py in <module>()
     54
     55 import matplotlib.tight_bbox as tight_bbox
---> 56 import matplotlib.textpath as textpath
     57 from matplotlib.path import Path
     58 from matplotlib.cbook import mplDeprecation
/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py in <module>()
     20 from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING
     21 from matplotlib.ft2font import LOAD_TARGET_LIGHT
 ---> 22 from matplotlib.mathtext import MathTextParser
     23 import matplotlib.dviread as dviread
     24 from matplotlib.font_manager import FontProperties

/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.py in <module>()
     61
     62 import matplotlib.colors as mcolors
---> 63 import matplotlib._png as _png
     64 ####################
     65
 ImportError: dlopen(/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/_png.so, 2): Library   not loaded: libpng15.15.dylib
  Referenced from: /Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/_png.so
  Reason: image not found

I've looked at Error importing matplotlib.pyplot which suggests deleting the matplotlib folder. I tried it and it didn't work. This also doesn't have a solution, but I think is related: Python: py2app "ImportError: dlopen(): Library not loaded"

Also i made sure I have libpng installed (via brew). Thanks,

Answer

Sean Zhu picture Sean Zhu · Mar 31, 2015

(sorry I can not comment yet.. so here's a new post)

I had the same problem when installing another library (spynner): dlopen(…) Library not loaded libpng15.15.dylib

I tried a similar method to @travelingbones's answer, and just wanted to add some notes for future readers:

  1. DYLD_LIBRARY_PATH should contain directories, not files. E.g.,

export DYLD_LIBRARY_PATH=/Users/xxx/anaconda/lib:$DYLD_LIBRARY_PATH

and not

export DYLD_LIBRARY_PATH=/Users/xxx/anaconda/lib/libpng15.15.dylib:$DYLD_LIBRARY_PATH

The error /usr/X11/lib/libpng15.15.dylib/libpng15.15.dylib: stat() failed with errno=20 is caused by the wrong DYLD_LIBRARY_PATH, since file path /usr/X11/lib/libpng15.15.dylib/libpng15.15.dylib does not exist (notice the double libpng15.15.dylib in the file path).

  1. After I had set the new DYLD_LIBRARY_PATH, I found my matplotlib was messed up, with the following error:
ImportError: dlopen(/Users/shenggao/anaconda/lib/python2.7/site-packages/matplotlib/backends/_macosx.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /Users/shenggao/anaconda/lib/libjpeg.8.dylib in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
  1. To solve this problem, I changed DYLD_LIBRARY_PATH back to the old value (i.e., empty), and added the libpng path to DYLD_FALLBACK_LIBRARY_PATH like this:

export DYLD_FALLBACK_LIBRARY_PATH=/Users/shenggao/anaconda/lib:$DYLD_FALLBACK_LIBRARY_PATH

Finally matplotlib and my new library worked like a charm!