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

Posted

技术标签:

【中文标题】import matplotlib.pyplot 给出 ImportError: dlopen(...) Library not loaded libpng15.15.dylib【英文标题】:import matplotlib.pyplot gives ImportError: dlopen(...) Library not loaded libpng15.15.dylib 【发布时间】:2015-02-01 14:51:57 【问题描述】:

导入pyplot报错:

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

我看过Error importing matplotlib.pyplot 这建议删除 matplotlib 文件夹。我试过了,但没有用。这也没有解决方案,但我认为是相关的:Python: py2app "ImportError: dlopen(): Library not loaded"

我还确定我已经安装了 libpng(通过 brew)。 谢谢,

【问题讨论】:

我也尝试通过 pip 卸载 matplot lib,然后 brew 重新安装 libpng 和 freetype,然后 pip 安装 matplotlib,我得到同样的错误......呃。 【参考方案1】:

(对不起,我还不能评论..所以这是一个新帖子)

我在安装另一个库(spynner)时遇到了同样的问题:dlopen(…) Library not loaded libpng15.15.dylib

我尝试了与@travelingbones 的答案类似的方法,只是想为未来的读者添加一些注释:

    DYLD_LIBRARY_PATH 应该包含目录,而不是文件。例如,

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

不是

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

错误/usr/X11/lib/libpng15.15.dylib/libpng15.15.dylib: stat() failed with errno=20是由错误的DYLD_LIBRARY_PATH引起的,因为文件路径/usr/X11/lib/libpng15.15.dylib/libpng15.15.dylib不存在(注意文件路径中的双libpng15.15.dylib)。

    设置新的 DYLD_LIBRARY_PATH 后,我发现我的 matplotlib 搞砸了,出现以下错误:
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
    为了解决这个问题,我将 DYLD_LIBRARY_PATH 改回旧值(即 empty),并将 libpng 路径添加到 DYLD_FALLBACK_LIBRARY_PATH 像这样:

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

终于 matplotlib 和我的新库像魅力一样工作了!

【讨论】:

酷!感谢分享。从那以后,我重新映像了我的 mac,并使用 brew 和 pip 重新安装了所有 python。从那以后一切都运行良好。如果您愿意擦除您的机器并从新的操作系统开始,我认为这是另一种解决方案。【参考方案2】:

我关注了these directions,得到了一个可以修复的不同错误: 首先,我检查了该路径中的内容:

macair93278:~ r8t$ echo $DYLD_LIBRARY_PATH

(empty)

接下来找到我需要的文件(libpng15.15.dylib):

macair93278:~ r8t$ locate libpng15.15.dylib
/Applications/KeePassX.app/Contents/MacOS/libpng15.15.dylib
/Users/r8t/anaconda/lib/libpng15.15.dylib
/Users/r8t/anaconda/pkgs/libpng-1.5.13-1/lib/libpng15.15.dylib
/usr/X11/lib/libpng15.15.dylib

现在我将底部三个附加到路径并检查路径:

macair93278:~ r8t$ echo $DYLD_LIBRARY_PATH  
/usr/X11/lib/libpng15.15.dylib:/Users/r8t/anaconda/pkgs/libpng-1.5.13-1/lib/libpng15.15.dylib:/Users/r8t/anaconda/lib/libpng15.15.dylib

好的,现在开始测试,

macair93278:~ r8t$ ipython

In [1]: import matplotlib

In [2]: from matplotlib import pyplot
…(bunch of output)
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: no suitable image found. Did find:
/usr/X11/lib/libpng15.15.dylib/libpng15.15.dylib: stat() failed with errno=20
/Users/r8t/anaconda/pkgs/libpng-1.5.13-1/lib/libpng15.15.dylib/libpng15.15.dylib: stat() failed with errno=20
/Users/r8t/anaconda/lib/libpng15.15.dylib/libpng15.15.dylib: stat() failed with errno=20

另一个错误!进步! 现在在this page 之后,我运行了 brew 链接,然后取消链接它并没有修复它,但它指出了 pyplot 正在 usr/local/lib 中寻找 libpng15.15.dylib 但它们不存在的事实:

macair93278:~ r8t$ cd ../usr/local/lib
macair93278:lib r8t$ ls *png*
libpng.a libpng16.16.dylib libpng16.dylib
libpng.dylib libpng16.a

所以把它们复制到那里:

macair93278:~ r8t$ cp ../anaconda/lib/*png* /usr/local/lib
macair93278:~ r8t$ cd /usr/local/lib

测试:

In [1]: import matplotlib 

In [2]: from matplotlib import pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Users/r8t/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in <module>
    from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends.

我们已将问题简化为这个(已解决的)问题:problem w/ mac os backend

【讨论】:

以上是关于import matplotlib.pyplot 给出 ImportError: dlopen(...) Library not loaded libpng15.15.dylib的主要内容,如果未能解决你的问题,请参考以下文章

Python库 - import matplotlib.pyplot as plt 报错问题

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

Mac OS Pycharm 上的 Python 通过“import matplotlib.pyplot as plt”给出框架错误

Python中用matplotlib.pyplot画图总结

numpy pandas matplotlib

机器学习Matplotlib 快速入门笔记