在Mac OS,Anaconda上导入matplotlib.pyplot时出现问题

时间:2018-02-13 01:43:32

标签: python matplotlib jupyter-notebook jupyter

我在Mac OS笔记本电脑上成功安装了Anaconda,Python2.7和matplotlib。

但是,当我导入matplotlib.pyplot时,我收到以下错误:

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
>>> 

以下是错误消息中提到的库的Conda版本:

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  

有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

可能的原因:第一次使用/ import matplotlib时,它(尝试)构建一个fontcache目录。这既缓慢又容易出错。

请参阅:matplotlib taking time when being imported

如果失败或使用了错误的权限,请删除所涉及的文件/目录并重新启动。

答案 1 :(得分:0)

我自己想通了。 Anaconda需要libz,我已经使用conda安装了该版本,并将其放置在Mac上的默认位置/Users/user.name/anaconda2/lib。但是,Python在libz中看到了我的Mac附带的/usr/lib/

我通过取消设置系统上的环境变量LD_LIBRARY_PATH修复了问题。这显然允许Anaconda找到正确的libz