无法在Mac OS X上安装matplotlib
我正在尝试在Mac OS X上为Python安装matplotlib,以便进行图形绘制。当我运行“python setup.py install”时,出现了一堆错误:http://pastebin.com/u7fL37ic。
这里有一小段错误信息:
src/ft2font.cpp:2170: error: ‘FT_LOAD_TARGET_MONO’ was not declared in this scope
src/ft2font.cpp:2171: error: ‘FT_LOAD_TARGET_LCD’ was not declared in this scope
src/ft2font.cpp:2172: error: ‘FT_LOAD_TARGET_LCD_V’ was not declared in this scope
src/ft2font.cpp:2175: error: ‘_ft2Library’ was not declared in this scope
src/ft2font.cpp:2175: error: ‘FT_Init_FreeType’ was not declared in this scope
src/ft2font.cpp: In destructor ‘virtual ft2font_module::~ft2font_module()’:
src/ft2font.cpp:2186: error: ‘_ft2Library’ was not declared in this scope
src/ft2font.cpp:2186: error: ‘FT_Done_FreeType’ was not declared in this scope
lipo: can't figure out the architecture type of: /var/folders/Nj/Njnlp9qSF64sMESWcaDnk++++TI/-Tmp-//cchyYmM5.out
error: command 'gcc-4.0' failed with exit status 1
我通过MacPorts安装了freetype,以为这样就能解决问题,但结果还是不行。还是出现之前的错误。看起来它找不到正确的freetype文件:
BUILDING MATPLOTLIB
matplotlib: 1.0.0
python: 2.6.5 (r265:79359, Mar 24 2010, 01:32:55) [GCC
4.0.1 (Apple Inc. build 5493)]
platform: darwin
REQUIRED DEPENDENCIES
numpy: 1.5.0
freetype2: found, but unknown version (no pkg-config)
* WARNING: Could not find 'freetype2' headers in any
* of '.', './freetype2'.
我应该把freetype文件放在哪里才能被找到呢?现在它们在/opt/local/lib这个路径下。
有什么建议吗?
14 个回答
这个问题虽然老旧,但我在搜索的时候还是遇到了同样的问题,特别是在使用Snow Leopard的时候。
你提到你在用homebrew,所以你需要在安装完之后执行一下
brew link freetype
(用“brew install freetype”来安装它)。
这样就能解决那个错误了。我在安装libpng的时候也遇到了类似的情况,执行了同样的步骤,最终成功安装了。
问题的根源在于,XCode把freetype和libpng安装在了不标准的位置,也就是放在了/usr/X11,而不是/usr或/usr/local。
之前给出的所有答案都通过重新构建freetype和libpng来解决这个问题,可以手动操作,也可以用像homebrew这样的包管理工具。
不过,你可以通过简单地将现有的freetype和libpng的头文件和库链接到/usr/local目录下,来让matplotlib编译成功,具体命令是:
sudo mkdir -p /usr/local/include
sudo ln -s /usr/X11/include/freetype2/freetype /usr/local/include/freetype
sudo ln -s /usr/X11/include/ft2build.h /usr/local/include/ft2build.h
sudo ln -s /usr/X11/include/png.h /usr/local/include/png.h
sudo ln -s /usr/X11/include/pngconf.h /usr/local/include/pngconf.h
sudo ln -s /usr/X11/include/pnglibconf.h /usr/local/include/pnglibconf.h
sudo mkdir -p /usr/local/lib
sudo ln -s /usr/X11/lib/libfreetype.dylib /usr/local/lib/libfreetype.dylib
sudo ln -s /usr/X11/lib/libpng.dylib /usr/local/lib/libpng.dylib
我更喜欢用pip来安装python包,所以接下来我会使用:
sudo pip install matplotlib
如果你还没有安装pip,可以通过easy_install来安装它(这个工具在OS X中自带):
sudo easy_install pip
我只在10.7(Lion)上测试过这个方法,但我怀疑在10.6上也能用。
这有点像是个小技巧,但我发现这是在OS X自带的python框架上安装matplotlib最简单的方法。10.7自带的python框架其实还不错,比如它包含了一个与Apple优化的BLAS库(Accelerate)链接的numpy-1.5.1包:
dyldinfo -dylibs /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/linalg/lapack_lite.so
for arch x86_64:
attributes dependent dylibs
/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate
/usr/lib/libSystem.B.dylib
你也可以考虑使用Enthought这个软件包(对于非商业用途,它是免费的)。这个软件包安装起来非常简单,没有任何麻烦。你可以在这里找到它:http://www.enthought.com/products/getepd.php。(我和Enthought没有任何关系,只是一个满意的用户。)