【问题标题】:Problems importing matplotlib.pyplot on Mac OS, Anaconda在 Mac OS、Anaconda 上导入 matplotlib.pyplot 时出现问题
【发布时间】:2018-02-13 01:43:32
【问题描述】:

我在我的 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  

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

【问题讨论】:

  • PYTHONPATH 应该没有设置,是吗?
  • 另外我似乎记得第一次导入 matplotlib 时有一些关于 fontcache 的废话。
  • 尝试重新安装? conda uninstall matplotlibconda install -c conda-forge matplotlib

标签: python matplotlib jupyter-notebook jupyter


【解决方案1】:

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

见:matplotlib taking time when being imported

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

【讨论】:

    【解决方案2】:

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

    我通过取消设置我系统上的环境变量LD_LIBRARY_PATH 解决了这个问题。这显然让 Anaconda 能够找到正确的 libz

    【讨论】:

      猜你喜欢
      • 2020-09-20
      • 2011-07-06
      • 2020-05-25
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多