安装matplotlib时出现错误

36

我尝试使用 pip install matplotlibgit clone 然后 python setup.py install,如安装常见问题所述,在Mac OS 10.7上安装matplotlib。但是我得到了相同的错误:

[...]
llvm-gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -DPY_ARRAY_UNIQUE_SYMBOL=MPL_ARRAY_API -DPYCXX_ISO_CPP_LIB=1 -I/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include -I. -I/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include/freetype2 -I./freetype2 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/ft2font.cpp -o build/temp.macosx-10.7-intel-2.7/src/ft2font.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
In file included from src/ft2font.cpp:3:
src/ft2font.h:16:22: error: ft2build.h: No such file or directory
src/ft2font.h:17:10: error: #include expects "FILENAME" or <FILENAME>
src/ft2font.h:18:10: error: #include expects "FILENAME" or <FILENAME>
src/ft2font.h:19:10: error: #include expects "FILENAME" or <FILENAME>
src/ft2font.h:20:10: error: #include expects "FILENAME" or <FILENAME>
src/ft2font.h:21:10: error: #include expects "FILENAME" or <FILENAME>
In file included from src/ft2font.cpp:3:
src/ft2font.h:34: error: ‘FT_Bitmap’ has not been declared
src/ft2font.h:34: error: ‘FT_Int’ has not been declared
src/ft2font.h:34: error: ‘FT_Int’ has not been declared
src/ft2font.h:86: error: expected ‘,’ or ‘...’ before ‘&’ token
[...]

看起来我在系统中缺少某些软件包?还有其他更好的安装matplotlib的方式吗?

谢谢!

更新:通过谷歌和搜索SO,我发现我可能缺少freetype2包,但是,如果我尝试通过Homebrew安装它,我会收到警告消息:

[me @ my mac]$ brew search freetype
Apple distributes freetype with OS X, you can find it in /usr/X11/lib.
However not all build scripts look here, so you may need to call ENV.x11
in your formula's install function.

1
我的错误信息可能不同,但在OSX 10.8.2上,运行brew install freetype然后pip install matplotlib解决了我的问题。 - hobs
2
@hobs:我还必须使用brew install libpng才能成功地pip install matplotlib。感谢Javier列出这些软件包。 - Bryan P
3个回答

80

我在Ubuntu服务器12.04上遇到了这个问题。

我必须从软件仓库安装libfreetype6-devlibpng-dev。当我使用pip安装matplotlib时,我正在使用virtualenv,然后遇到了这个问题。

提示我需要这样做的线索来自于matplotlib安装早期弹出的警告消息,因此请注意这些指示依赖项已经找到,但是没有头文件。


这里的指针真是太棒了。感谢列出这些包,让我立刻解决了我的问题。 - bitcycle
1
谢谢,那个完美地运行了(Ubuntu 12.04,Python 2.7.3,在虚拟环境中)。 - Balthazar Rouberol
2
太棒了。这帮了大忙。在OS X上,你可以使用“brew install freetype”。 - Jeremy Mullin
2
在Centos 6上,可以使用yum安装freetype-devel和libpng-devel。 - Adrian Mester
1
使用以下命令以解决依赖关系:sudo apt-get build-dep python-matplotlib - HackNone
1
那么你是如何安装这两个包的呢?使用apt-get还是pip?我也在虚拟环境中,使用的是Ubuntu Server 12.04。 - VaidAbhishek

13

遇到了相同的错误,在我的两台 Lion 机器上安装都可以,但在其中一台上无法安装。跟踪发现是缺少了 pkg-config。

$ brew install pkg-config
$ pip install -U 'http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.1.0/matplotlib-1.1.0.tar.gz/download'

(参见https://jholewinski.org/blog/installing-matplotlib-on-os-x-10-7-with-homebrew/)


链接似乎已被移除。顺便问一下,在OS X 10.8 Mountain Lion中安装pkg-config有什么区别吗?我在brew安装pkg-config时遇到了错误。 - clwen
1
更新:安装了X11之后,可以安装pkg-config。然后可以从GitHub下载matplotlib的源代码并从源代码构建。 - clwen

2

谢谢您提供的链接。我进行了一些微小的更改,成功地使matplotlib工作。为了以后的参考,我记录了我遇到的具体错误信息。

环境: Mac OS X 10.7.4(Lion),使用默认安装的Python 2.7.1(位于/usr/bin中)

我最初尝试使用默认安装来让matplotlib工作。但setup.py提示了一些问题,具体如下:

freetype2: found, but unknown version (no pkg-config)
                        * WARNING: Could not find 'freetype2' headers in any
                        * of '.', './freetype2'.

并且
OPTIONAL BACKEND DEPENDENCIES
                libpng: found, but unknown version (no pkg-config)
                        * Could not find 'libpng' headers in any of '.'

安装步骤会失败,出现以下错误信息:
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include/numpy/__multiarray_api.h:1187: warning: ‘int _import_array()’ defined but not used
lipo: can't open input file: /var/tmp//ccG28dDI.out (No such file or directory)

在Google上搜索这个文件并没有帮助我解决问题。我稍微查了一下,决定不值得我花时间去尝试virtualenv。

最终,我按照上面jholewinski的链接中概述的步骤进行操作。我在/usr/local/中安装了一个新的Python 2.7.4,并按照那里的说明进行了操作(我重新安装了pkg-config)。我能够从git上拉取最新版本的matplotlib,并且它正常工作。

这次,libpng(1.5.4)和freetype2(13.2.7)库都存在。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接