Python 无法在 Mac OS X 上安装 matplotlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4092994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:12:31  来源:igfitidea点击:

Unable to install matplotlib on Mac OS X

pythonmacosmatplotlibmacportsfreetype

提问by tchaymore

I'm trying to install matplotlib for graphing applications in Python on Mac OS X. When I run "python setup.py install", it gives me this load of errors: http://pastebin.com/u7fL37ic.

我正在尝试在 Mac OS X 上安装用于在 Python 中绘制应用程序的 matplotlib。当我运行“python setup.py install”时,它给了我很多错误:http: //pastebin.com/u7fL37ic

A quick snippet:

一个快速片段:

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

I installed freetype using MacPorts, and I thought that would fix the issue, but no luck. Gives me same error as before. It looks like it can't find the right freetype files:

我使用 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'.

Where should I put the freetype files so that they can be found? Right now they're in /opt/local/lib

我应该把 freetype 文件放在哪里才能找到它们?现在他们在 /opt/local/lib

Any ideas?

有任何想法吗?

采纳答案by mad7777

you may also consider using the Enthought distribution (which comes even free for non-commercial usage) It comes fully installed without any difficulty http://www.enthought.com/products/getepd.php(I have no relationship with Enthought whatsoever - I'm just a satisfied user)

你也可以考虑使用 Enthought 发行版(它甚至免费提供给非商业用途)它完全安装没有任何困难 http://www.enthought.com/products/getepd.php(我与 Enthought 没有任何关系 -我只是一个满意的用户)

回答by telliott99

Try the instuctions here. My notes here.

试试这里的说明。我的笔记在这里

回答by rytis

Here's how I did it:

这是我如何做到的:

$ sudo make -f make.osx fetch deps mpl_build mpl_install

This will install it in /lib/python2.6/site-packages, just move its contents to /Library/Python/2.6/site-packages/for consistency.

这会将其安装在 中/lib/python2.6/site-packages,只需将其内容移动到/Library/Python/2.6/site-packages/以保持一致性。

回答by davebiagioni

I was given a new work computer recently (MacBook Pro OS 10.6.5), and wanted to install numpy + scipy + matplotlib in a Python virtual environment. I'm not an expert, but the virtual environment seems to allow you to install packages such that they are isolated from the system-wide packages, essentially by redefining the system path in a clever way. So, for one project I am working on that requires certain versions of these packages, I can install them once and do all of my work for that project there, independently of other changes I make outside the environment.

最近收到了一台新的工作电脑(MacBook Pro OS 10.6.5),想在Python虚拟环境中安装numpy + scipy + matplotlib。我不是专家,但虚拟环境似乎允许您安装软件包,使它们与系统范围的软件包隔离,主要是通过以巧妙的方式重新定义系统路径。因此,对于我正在处理的一个需要这些包的某些版本的项目,我可以安装它们一次并在那里为该项目完成我的所有工作,而与我在环境之外所做的其他更改无关。

After a lot of trial and error, I was able to build a virtual environment with Numpy 1.5.1, Scipy 0.8.0, and Matplotlib 1.0.0 all running flawlessly. Here's how:

经过大量的反复试验,我能够使用 Numpy 1.5.1、Scipy 0.8.0 和 Matplotlib 1.0.0 构建一个虚拟环境,它们都可以完美运行。就是这样:

First, install the following four packages:

首先,安装以下四个包:

(I'm a new user so I can't post links to these, sorry!).

(我是新用户,所以我不能发布这些链接,抱歉!)。

If you install from source, use a standard install:

如果从源安装,请使用标准安装:

  • $ ./configure
  • $ make
  • $ sudo make install
  • $ ./配置
  • $ make
  • $ sudo 安装

Next, download the source files for numpy, scipy, and matplotlib.

接下来,下载 numpy、scipy 和 matplotlib 的源文件。

Now it's time to create a virtual environment in folder TESTENV:

现在是在文件夹 TESTENV 中创建虚拟环境的时候了:

$ virtualenv /path/to/dir/TESTENV

$ virtualenv /path/to/dir/TESTENV

Activate the virtual environment:

激活虚拟环境:

$ source . /path/to/dir/TESTENV/bin/activate

$ 来源。/path/to/dir/TESTENV/bin/activate

Now, python packages will be installed within TESTENV as if it were the root installation directory. What worked for me was to execute:

现在,python 包将安装在 TESTENV 中,就像它是根安装目录一样。对我有用的是执行:

$ python setupegg.py install

$ python setupegg.py 安装

in the numpy, scipy, and matplotlib source folders (in that order).

在 numpy、scipy 和 matplotlib 源文件夹中(按此顺序)。

Hope that helps!

希望有帮助!

回答by Jonathan Berger

I would highly recommend the StrongInference Scipy Superpack. It works with the built-in Apple supplied Python on 10.6 Snow Leopard, and this is now working with 10.9. It's friendly to both 32-bit and 64-bit installs. It does not require installing a separate Python distribution or package managers like Macports or Fink.

我强烈推荐StrongInference Scipy Superpack。它适用于 10.6 Snow Leopard 上的内置 Apple 提供的 Python,现在适用于 10.9。它对 32 位和 64 位安装都很友好。它不需要安装单独的 Python 发行版或 Macports 或 Fink 等包管理器。

Use it by first downloading the shell script from this page. Then:

首先从这个页面下载 shell 脚本来使用它。然后:

sh superpack_10.6_2011.03.07.sh

Or, replace superpack_10.6_2011.03.07.shwith the name of the latest shell script as they update it from time to time.

或者,替换superpack_10.6_2011.03.07.sh为最新的 shell 脚本的名称,因为它们会不时更新。

To use it, make sure you type in ipython and not python!

要使用它,请确保输入 ipython 而不是 python!

回答by David Xia

I followed this page's instructions. I got stuck at

我按照此页面的说明进行操作。我被困在

pip install -e git+https://github.com/matplotlib/matplotlib#egg=matplotlib-dev

Then I did:

然后我做了:

git clone https://github.com/matplotlib/matplotlib.git
cd matplotlib
python setup.py build
python setup.py install

Checked my installation by typing in terminal:

通过在终端中输入来检查我的安装:

python
import matplotlib
print matplotlib.__version__
print matplotlib.__file__

I got version 0.10.0 dev (as of this writing) and path /usr/local/Cellar/...

我得到了版本 0.10.0 dev(在撰写本文时)和路径 /usr/local/Cellar/...

回答by dsummersl

I like using the brew, and none of the top three explanations worked or were enticing to me. However, I did look some more, and found this brew friendly explanation and solution:

我喜欢使用 brew,前三个解释都没有奏效或对我有吸引力。但是,我确实多看了一些,发现了这个 brew 友好的解释和解决方案:

http://jholewinski.org/blog/installing-matplotlib-on-os-x-10-7-with-homebrew/

http://jholewinski.org/blog/installing-matplotlib-on-os-x-10-7-with-homebrew/

Summary: libpng on Snow Leopard isn't on friendly terms with matplotlib. You can manually install the un-released matplotlib to work around it:

总结:Snow Leopard 上的 libpng 与 matplotlib 并不友好。您可以手动安装未发布的 matplotlib 来解决它:

brew install python
brew install gfortran
brew install pkg-config
easy_install pip
pip install numpy
git clone https://github.com/matplotlib/matplotlib.git
cd matplotlib
python setup.py build
python setup.py install

回答by pohChiv6

I got a similar error - you're missing the development files for libfreetype - on my linux machine - this is libfreetype6-dev

我有一个类似的错误 - 你缺少 libfreetype 的开发文件 - 在我的 linux 机器上 - 这是 libfreetype6-dev

After I installed this pip did it's job.

在我安装了这个 pip 之后,它就完成了。

回答by mhowison

The root of the problem is that freetype and libpng are installed in non-canonical locations by XCode, in /usr/X11 instead of /usr or /usr/local.

问题的根源在于 XCode 将 freetype 和 libpng 安装在非规范位置,即 /usr/X11 而不是 /usr 或 /usr/local。

All of the answers already given address the problem by re-building freetype and libpng, either manually or using a package manager like homebrew.

已经给出的所有答案都通过手动或使用像 homebrew 这样的包管理器重新构建 freetype 和 libpng 来解决问题。

You can, however, get matplotlib to compile by simply symlinking the existing freetype/libpng headers and libraries into the /usr/local tree with:

但是,您可以通过简单地将现有的 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

I prefer to build python packages with pip, so I would then use:

我更喜欢用 pip 构建 python 包,所以我会使用:

sudo pip install matplotlib

If you don't already have pip, you can install it with easy_install (which comes with OS X):

如果您还没有 pip,您可以使用 easy_install(OS X 附带的)安装它:

sudo easy_install pip

I've only tested this on 10.7 (Lion) but I suspect it will also work with 10.6.

我只在 10.7 (Lion) 上测试过,但我怀疑它也适用于 10.6。

It is a little bit of a hack, but I've found it to be the easiest way to get matplotlib installed against the stock python framework that ships with OS X. The stock python framework in 10.7 is actually pretty good, and includes, for instance, a numpy-1.5.1 package that is linked against Apple's optimized BLAS library (Accelerate):

这有点像 hack,但我发现它是针对 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

回答by SeanZZZ

Old, but still popped up in my search when I had the same problem on Snow Leopard.

旧的,但当我在 Snow Leopard 上遇到同样的问题时,它仍然出现在我的搜索中。

You said you were using homebrew, so you need to

你说你用的是自制软件,所以你需要

brew link freetype

after installing it (with "brew install freetype").

安装后(使用“brew install freetype”)。

This got through that error. I had do the same thing with libpng, which resulted in a successful install.

这克服了那个错误。我对 libpng 做了同样的事情,结果安装成功。