【问题标题】:Error while trying to install matplotlib on Mac尝试在 Mac 上安装 matplotlib 时出错
【发布时间】:2023-12-12 12:37:02
【问题描述】:

我已经下载了 Python 3.7,并正在使用我的终端将 matplotlib 安装到我的 Mac OS 上。我试过了:

pip3 install matplotlib

但是,我遇到了以下错误:

        ERROR: Command errored out with exit status 1:
     command: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/fm/gp4lphn1231f22hp_kzh82rw0000gn/T/pip-install-n_oqiwm0/matplotlib/setup.py'"'"'; __file__='"'"'/private/var/folders/fm/gp4lphn1231f22hp_kzh82rw0000gn/T/pip-install-n_oqiwm0/matplotlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/fm/gp4lphn1231f22hp_kzh82rw0000gn/T/pip-record-_jw4chgf/install-record.txt --single-version-externally-managed --compile
         cwd: /private/var/folders/fm/gp4lphn1231f22hp_kzh82rw0000gn/T/pip-install-n_oqiwm0/matplotlib/
    Complete output (506 lines):
    ================================================================================
    Edit setup.cfg to change the build options

    BUILDING MATPLOTLIB
      matplotlib: yes [3.1.1]
          python: yes [3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)  [Clang 6.0
                      (clang-600.0.57)]]
        platform: yes [darwin]

    OPTIONAL SUBPACKAGES
     sample_data: yes [installing]
           tests: no  [skipping due to configuration]

    OPTIONAL BACKEND EXTENSIONS
             agg: yes [installing]
           tkagg: yes [installing; run-time loading from Python Tcl/Tk]
          macosx: yes [installing, darwin]

    OPTIONAL PACKAGE DATA
            dlls: no  [skipping due to configuration]
    ...

    UPDATING build/lib.macosx-10.9-x86_64-3.8/matplotlib/_version.py
    set build/lib.macosx-10.9-x86_64-3.8/matplotlib/_version.py to '3.1.1'
    running build_ext
    building 'matplotlib.ft2font' extension
    creating build/temp.macosx-10.9-x86_64-3.8
    creating build/temp.macosx-10.9-x86_64-3.8/src
    gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch x86_64 -g -DFREETYPE_BUILD_TYPE=system -DPY_ARRAY_UNIQUE_SYMBOL=MPL_matplotlib_ft2font_ARRAY_API -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -D__STDC_FORMAT_MACROS=1 -Iextern/agg24-svn/include -I/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/include -I/Library/Frameworks/Python.framework/Versions/3.8/include/python3.8 -c src/checkdep_freetype2.c -o build/temp.macosx-10.9-x86_64-3.8/src/checkdep_freetype2.o
    src/checkdep_freetype2.c:1:10: fatal error: 'ft2build.h' file not found
    #include <ft2build.h>
             ^~~~~~~~~~~~
    1 error generated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/fm/gp4lphn1231f22hp_kzh82rw0000gn/T/pip-install-n_oqiwm0/matplotlib/setup.py'"'"'; __file__='"'"'/private/var/folders/fm/gp4lphn1231f22hp_kzh82rw0000gn/T/pip-install-n_oqiwm0/matplotlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/fm/gp4lphn1231f22hp_kzh82rw0000gn/T/pip-record-_jw4chgf/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.

我不知道为什么它不会安装。 3.7和3.8我都试过了;但是,似乎没有任何效果。

【问题讨论】:

  • 这可能与您的编译器有关。你有什么编译器?
  • 您能否复制粘贴更多您在终端上看到的错误日志?
  • 现在最好不要尝试在 python 3.8 上安装 matplotlib 3.1。有各种各样的问题源于需要从源代码构建它。暂时使用python 3.7,防止编译任何东西。

标签: python python-3.x macos matplotlib pip


【解决方案1】:

您缺少从头开始编译 Matplotlib 所需的 C 编译器。

通常,Matplotlib 和许多其他更复杂的库以二进制形式作为 wheels 分发。

但是,您提到您使用的是 Python 3.8,它是非常 最新的。不幸的是,在为 Python 3.8 编写 there are no wheels for the latest release of Matplotlib 时。

不过,它们确实存在于latest pre-release version 3.2.0a1

如果你愿意试用那个版本,

pip3 install --pre matplotlib==3.2.0a1

或者直接安装 Python 3.7。

【讨论】:

  • 我尝试了这两个建议,但都没有奏效。