You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm安装scikit-learn失败求助:缺失Fortran编译器报错

在PyCharm中安装scikit-learn失败的求助

我在PyCharm中安装scikit-learn时遭遇失败,报错详情如下:

Collecting scikit-learn
  Using cached scikit-learn-1.2.2.tar.gz (7.3 MB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'error'

  error: subprocess-exited-with-error
  
  × pip subprocess to install build dependencies did not run successfully.
  │ exit code: 1
  ╰─> [74 lines of output]
      Ignoring numpy: markers 'python_version == "3.10" and platform_system == "Windows" and platform_python_implementation != "PyPy"' don't match your environment
      Collecting setuptools
        Using cached setuptools-67.8.0-py3-none-any.whl (1.1 MB)
      Collecting wheel
        Using cached wheel-0.40.0-py3-none-any.whl (64 kB)
      Collecting Cython>=0.29.24
        Using cached Cython-0.29.35-py2.py3-none-any.whl (988 kB)
      Collecting oldest-supported-numpy
        Using cached oldest_supported_numpy-2022.11.19-py3-none-any.whl (4.9 kB)
      Collecting scipy>=1.3.2
        Using cached scipy-1.10.1.tar.gz (42.4 MB)
        Installing build dependencies: started
        Installing build dependencies: finished with status 'done'
        Getting requirements to build wheel: started
        Getting requirements to build wheel: finished with status 'done'
        Installing backend dependencies: started
        Installing backend dependencies: finished with status 'done'
        Preparing metadata (pyproject.toml): started
        Preparing metadata (pyproject.toml): finished with status 'error'
        error: subprocess-exited-with-error
      
        × Preparing metadata (pyproject.toml) did not run successfully.
        │ exit code: 1
        ╰─> [40 lines of output]
            The Meson build system
            Version: 1.1.1
            Source dir: /private/var/folders/c5/7bp39l255q5c7bf9j_8t2s8c0000gn/T/pip-install-sju6xn7_/scipy_bcf1716e49654d2db8011d8ebda0da5f
            Build dir: /private/var/folders/c5/7bp39l255q5c7bf9j_8t2s8c0000gn/T/pip-install-sju6xn7_/scipy_bcf1716e49654d2db8011d8ebda0da5f/.mesonpy-jflrrsl0/build
            Build type: native build
            Project name: SciPy
            Project version: 1.10.1
            C compiler for the host machine: cc (clang 12.0.5 "Apple clang version 12.0.5 (clang-1205.0.22.9)")
            C linker for the host machine: cc ld64 650.9
            C++ compiler for the host machine: c++ (clang 12.0.5 "Apple clang version 12.0.5 (clang-1205.0.22.9)")
            C++ linker for the host machine: c++ ld64 650.9
            Cython compiler for the host machine: cython (cython 0.29.35)
            Host machine cpu family: aarch64
            Host machine cpu: aarch64
            Compiler for C supports arguments -Wno-unused-but-set-variable: NO
            Compiler for C supports arguments -Wno-unused-function: YES
            Compiler for C supports arguments -Wno-conversion: YES
            Compiler for C supports arguments -Wno-misleading-indentation: YES
            Compiler for C supports arguments -Wno-incompatible-pointer-types: YES
            Library m found: YES
      
            ../../meson.build:63:0: ERROR: Unknown compiler(s): [['gfortran'], ['flang'], ['nvfortran'], ['pgfortran'], ['ifort'], ['ifx'], ['g95']]
            The following exception(s) were encountered:
            Running `gfortran --version` gave "[Errno 2] No such file or directory: 'gfortran'"
            Running `gfortran -V` gave "[Errno 2] No such file or directory: 'gfortran'"
            Running `flang --version` gave "[Errno 2] No such file or directory: 'flang'"
            Running `flang -V` gave "[Errno 2] No such file or directory: 'flang'"
            Running `nvfortran --version` gave "[Errno 2] No such file or directory: 'nvfortran'"
            Running `nvfortran -V` gave "[Errno 2] No such file or directory: 'nvfortran'"
            Running `pgfortran --version` gave "[Errno 2] No such file or directory: 'pgfortran'"
            Running `pgfortran -V` gave "[Errno 2] No such file or directory: 'pgfortran'"
            Running `ifort --version` gave "[Errno 2] No such file or directory: 'ifort'"
            Running `ifort -V` gave "[Errno 2] No such file or directory: 'ifort'"
            Running `ifx --version` gave "[Errno 2] No such file or directory: 'ifx'"
            Running `ifx -V` gave "[Errno 2] No such file or directory: 'ifx'"
            Running `g95 --version` gave "[Errno 2] No such file or directory: 'g95'"
            Running `g95 -V` gave "[Errno 2] No such file or directory: 'g95'"
      
            A full log can be found at /private/var/folders/c5/7bp39l255q5c7bf9j_8t2s8c0000gn/T/pip-install-sju6xn7_/scipy_bcf1716e49654d2db8011d8ebda0da5f/.mesonpy-jflrrsl0/build/meson-logs/meson-log.txt
            + meson setup --prefix=/Library/Frameworks/Python.framework/Versions/3.11 /private/var/folders/c5/7bp39l255q5c7bf9j_8t2s8c0000gn/T/pip-install-sju6xn7_/scipy_bcf1716e49654d2db8011d8ebda0da5f /private/var/folders/c5/7bp39l255q5c7bf9j_8t2s8c0000gn/T/pip-install-sju6xn7_/scipy_bcf1716e49654d2db8011d8ebda0da5f/.mesonpy-jflrrsl0/build --native-file=/private/var/folders/c5/7bp39l255q5c7bf9j_8t2s8c0000gn/T/pip-install-sju6xn7_/scipy_bcf1716e49654d2db8011d8ebda0da5f/.mesonpy-native-file.ini -Ddebug=false -Doptimization=2
            [end of output]
      
        note: This error originates from a subprocess, and is likely not a problem with pip.
      error: metadata-generation-failed
      
      × Encountered error while generating package metadata.
      ╰─> See above for output.
      
      note: This is an issue with the package mentioned above, not pip.
      hint: See above for details.
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× pip subprocess to install build dependencies did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

我已尝试通过brew、多种终端命令以及PyCharm手动安装scikit-learn,但所有操作均以报错告终,恳请各位提供解决方法。


解决方法

核心原因

报错关键在于系统缺少Fortran编译器(gfortran),scipy编译依赖该工具,而scikit-learn又依赖scipy,导致安装链断裂。

具体解决方案

方案1:安装gfortran后重新用pip安装

  • 用Homebrew安装gcc(包含gfortran):
    brew install gcc
    
  • 验证安装成功:
    gfortran --version
    
  • 激活PyCharm对应的虚拟环境,重新执行安装命令:
    pip install scikit-learn
    

方案2:使用conda安装预编译包(避免本地编译)

如果用conda管理环境,直接安装预编译的scikit-learn包,无需处理编译依赖:

  • 激活目标conda环境:
    conda activate your_env_name
    
  • 安装scikit-learn:
    conda install scikit-learn
    

方案3:指定安装scikit-learn的兼容预编译wheel包

针对Apple Silicon(aarch64),可以直接安装适配的预编译包:

  • 先升级pip到最新版:
    pip install --upgrade pip
    
  • 安装scikit-learn:
    pip install scikit-learn --only-binary :all:
    

(--only-binary :all: 强制pip使用预编译包,跳过本地编译)

内容的提问来源于stack exchange,提问作者mmm coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:57:41