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

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I am aware that this exact same question has been asked before. I did follow the instructions given in the answer there, and it didn't solve my problem (and I don't have enough reputation to just comment on the Q or A in that thread). Anyway, here's what's going on:

I try to do:

import matplotlib.pyplot 

And in return I get:

Traceback (most recent call last):   File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3032, in run_code     exec(code_obj, self.user_global_ns, self.user_ns)   File "", line 1, in      import matplotlib.pyplot as plt   File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in      import matplotlib.colorbar   File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in      import matplotlib.collections as collections   File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in      import matplotlib.backend_bases as backend_bases   File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in      import matplotlib.textpath as textpath   File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 22, in      from matplotlib.mathtext import MathTextParser   File "/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.py", line 63, in      import matplotlib._png as _png ImportError: dlopen(/Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/_png.so, 2): Library not loaded: libpng15.15.dylib   Referenced from: /Users/russellrichie/anaconda/lib/python2.7/site-packages/matplotlib/_png.so   Reason: image not found 

My Python version:

2.7.7 |Anaconda 2.0.1 (x86_64)| (default, Jun  2 2014, 12:48:16) [GCC 4.0.1 (Apple Inc. build 5493)] 

EDIT:

cel's suggestion worked! I just tried "conda remove matplotlib", "pip install matplotlib", and then "conda install matplotlib", and presto! Man, you have no idea how long this problem has vexed me. Bless you all.

回答1:

Some python packages link dynamically against native c libraries. After an update of one of those libraries, links can break and give you weird error messages about missing dynamic libraries, as seen in the error message in the question.

Basically, after an update of a native library sometimes you also have to rebuild python packages (here matplotlib).

The above statement is true in general. If you are using conda as your python distribution things are usually less complicated:

For extension packages conda also maintains required c libraries. As long as you use only conda install and conda update for installing those packages you should not run into these issues.

For numpy, scipy, matplotlib and many more I would suggest to try conda search first to see if there's a conda recipe that matches your needs. For most users conda install will be a better option than pip install.

To make sure that only conda's version is installed you can do

conda remove matplotlib pip uninstall matplotlib conda install matplotlib 

Afterwards this issue should not appear anymore.



回答2:

I ran into this problem as well. I updated my Anaconda-Navigator and the next time I opened a project with matplotlib.pyplot, I ran into a similar problem. What worked for me was:

conda install libpng 


回答3:

I had this problem, but it was because I had set

export DYLD_LIBRARY_PATH="/Users/charlesmartin14/anaconda/lib":$DYLD_LIBRARY_PATH

removing this setting and restarting the shell fixed it



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!