Matplotlib错误:libfreetype.6.dylib

Rob*_*pps 1 python matplotlib dylib freetype

我正在尝试运行以下python脚本(我正在使用Vim):

import numpy as np;
import scipy as sp;
from scipy import misc;
import matplotlib.pyplot as plt;
image = misc.imread('test_image.jpg');
np.fliplr(image);
plt.imshow(image);
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到以下内容:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    import matplotlib.pyplot as plt;
  File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 24, in <module>
    import matplotlib.colorbar
  File "/Library/Python/2.7/site-packages/matplotlib/colorbar.py", line 29, in <module>
    import matplotlib.collections as collections
  File "/Library/Python/2.7/site-packages/matplotlib/collections.py", line 23, in <module>
    import matplotlib.backend_bases as backend_bases
  File "/Library/Python/2.7/site-packages/matplotlib/backend_bases.py", line 50, in <module>
    import matplotlib.textpath as textpath
  File "/Library/Python/2.7/site-packages/matplotlib/textpath.py", line 11, in <module>
    import matplotlib.font_manager as font_manager
  File "/Library/Python/2.7/site-packages/matplotlib/font_manager.py", line 53, in <module>
    from matplotlib import ft2font
ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib/ft2font.so, 2): Library not loaded: @loader_path/../../../libfreetype.6.dylib
  Referenced from: /Library/Python/2.7/site-packages/matplotlib/ft2font.so
  Reason: image not found
shell returned 1
Run Code Online (Sandbox Code Playgroud)

我尝试重新安装brew,重新安装freetype,matplotlib以及numpybrew和我的MacPorts卸载与错误没有变化.建议?

编辑:卸载MacPorts然后再重新安装brew,我现在得到此错误.

Fatal Python error: PyThreadState_Get: no current thread Command terminated

这个错误只出现在我import matplotlib身上,所以我猜测问题就在于此matplotlib.我会尝试重新安装它brew.

编辑2:我一直在尝试从这个页面无济于事,但我认为我的错误可能与那个错误有关.

Sha*_*Dou 9

[MacOS X]安装后遇到同样的问题graphviz(可视化决策树).如果没有仔细隔离环境,这个新包似乎已将自己喜欢的freetype版本放入我的默认python运行时库路径中.然后在进行简单导入时import matplotlib.pyplot as plt,我收到错误消息:

ImportError: dlopen(/Users/shandou/anaconda3/lib/python3.6/site-
packages/matplotlib/ft2font.cpython-36m-darwin.so, 2): Library not 
loaded: @rpath/libfreetype.6.dylib
Referenced from: /Users/shandou/anaconda3/lib/python3.6/site- 
packages/matplotlib/ft2font.cpython-36m-darwin.so
Reason: Incompatible library version: ft2font.cpython-36m-darwin.so 
requires version 22.0.0 or later, but libfreetype.6.dylib provides 
version 21.0.0
Run Code Online (Sandbox Code Playgroud)

起初,我无法理解@rpath真正指向的是什么.检查locate libfreetype并看起来像我的默认python环境,我有(1)/Users/shandou/anaconda3/lib/libfreetype.6.dylib和(2)/Users/shandou/anaconda3/pkgs/freetype-2.8.1-0

我尝试了以下两个修复程序.第一个解决了使matplotlib导入工作的迫切需要,但后来在sphinx auto doc生成中引起了问题.第二个是更清洁的修复,使两者都工作.


修复1:conda卸载然后pip安装matplotlib

  • 上下文:我使用anaconda python发行版并使用conda作为我的主程序包管理器
  • 扰流板警报:暂时修复了导入问题,但是在使用sphinx后我遇到了麻烦
  • 外卖:混合pip和conda安装主要库可能会有问题

按照@Robbie Capps上面的建议,我卸载了最初安装了conda的matplotlib,并用pip重新安装了它.之后matplotlib导入工作正常,我能够继续工作,直到后来我在运行sphinx记录代码时遇到错误:

File "/Users/shandou/anaconda3/lib/python3.6/site-
packages/matplotlib/backends/backend_macosx.py", line 17, 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. If you are using (Ana)Conda please install python.app and 
replace the use of 'python' with 'pythonw'. See 'Working with 
Matplotlib on OSX' in the Matplotlib FAQ for more information. 
Run Code Online (Sandbox Code Playgroud)

这看起来很毛茸茸,但是如果我读得正确的话,信息的要点是:sphinx对我混合conda和pip安装不满意.

所以我最终将matplotlib恢复为conda安装.可悲的是原来的libfreetype错误立即返回,我无法做基本的matplotlib导入(darn ...)


修复2:更新位于运行时路径中的freetype库(错误消息中的@rpath)

  • 上下文:我尝试更新freetype,libpng和matplotlib - 基本上是网络建议的任何内容
  • 扰流器警报:如果未更新运行时路径库,我将收到有关不兼容的libfreetype的相同错误消息

[步骤1] Brew安装freetype库:

$ brew install freetype
$ brew link --overwrite freetype 
Run Code Online (Sandbox Code Playgroud)

检查库版本时/usr/local/Cellar/freetype/2.9/lib/,我得到如下输出:

$ otool -L libfreetype.6.dylib | head -n 2
libfreetype.6.dylib:
/usr/local/opt/freetype/lib/libfreetype.6.dylib (compatibility version 23.0.0, current version 23.0.0)
Run Code Online (Sandbox Code Playgroud)

这是版本21+,所以我们离解决问题更近了一步

[步骤2]复制/usr/local/Cellar/freetype/2.9/lib/libfreetype.6.dylib到python运行时库路径

事实证明,即使用conda更新freetype库,运行时库也不会更新.对我有用的最终解决方案是强制将较新的freetype lib复制到运行时路径:

$ cd /Users/shandou/anaconda3/lib/
$ sudo cp /usr/local/Cellar/freetype/2.9/lib/libfreetype.6.dylib .
Run Code Online (Sandbox Code Playgroud)

只有这样,freetype库版本不兼容问题就消失了,matplotlib import和sphinx都很开心


底线:修复2是更清洁的方式.