Problems importing matplotlib.pyplot on Mac OS, Anaconda

I successfully installed Anaconda, Python2.7, and matplotlib on my Mac OS laptop.

However, when I import matplotlib.pyplot, I get the following errors:

Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct  5 2017, 02:28:52)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 32, in <module>
    import matplotlib.colorbar
  File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
    import matplotlib.contour as contour
  File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/contour.py", line 21, in <module>
    import matplotlib.font_manager as font_manager
  File "/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py", line 58, in <module>
    from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
ImportError: dlopen(/Users/user.name/anaconda2/lib/python2.7/site-packages/matplotlib/ft2font.so, 2): Symbol not found: _inflateValidate
  Referenced from: /Users/user.name/anaconda2/lib/libpng16.16.dylib
  Expected in: /usr/lib/libz.1.dylib
 in /Users/user.name/anaconda2/lib/libpng16.16.dylib
>>> 

Here are the Conda versions of the libraries mentioned above in the error messages:

conda list matplotlib
# packages in environment at /Users/user.name/anaconda2:
#
# Name                    Version                   Build  Channel
matplotlib                2.1.2            py27h6d6146d_0  


conda list libpng
# packages in environment at /Users/user.name/anaconda2:
#
# Name                    Version                   Build  Channel
libpng                    1.6.34               he12f830_0  


conda list zlib
# packages in environment at /Users/user.name/anaconda2:
#
# Name                    Version                   Build  Channel
zlib                      1.2.11               hf3cbc9b_2  

Does anyone know how to fix this problem?

2 answers

  • answered 2018-02-13 02:17 smci

    Possible cause: the first time you use/import matplotlib, it (tries to) builds a fontcache directory. This is both slow and error-prone.

    See: matplotlib taking time when being imported

    If it fails or uses wrong permissions, delete the files/directories involved and restart.

  • answered 2018-02-13 02:35 stackoverflowuser2010

    I figured it out myself. Anaconda needed libz, which I had already installed with conda and was placed into the default location /Users/user.name/anaconda2/lib on my Mac. However, Python was seeing the libz that came with my Mac in /usr/lib/.

    I fixed the problem by unsetting the environment variable LD_LIBRARY_PATH on my system. That apparently allowed Anaconda to find the correct libz.