如何在M1 Mac的Python3.11环境下安装scikit-learn==0.23.1?
问题:在M1 MacBook Air上安装scikit-learn==0.23.1失败,求解决方法
我需要安装scikit-learn==0.23.1(或更早版本)以确保项目兼容AWS Lambda层。尝试过pip3 install scikit-learn==0.23.1和pip3 install -U scikit-learn==0.23.1 --no-cache-dir均失败。
我的环境:
- M1 MacBook Air
- Python版本:3.11.1
执行pip3 install scikit-learn==0.23.1的错误日志:
Collecting scikit-learn==0.23.1 Using cached scikit-learn-0.23.1.tar.gz (7.2 MB) Installing build dependencies ... error error: subprocess-exited-with-error × pip subprocess to install build dependencies did not run successfully. │ exit code: 1 ╰─> [107 lines of output] Ignoring numpy: markers 'python_version == "3.6" and platform_system != "AIX" and platform_python_implementation == "CPython"' don't match your environment Ignoring numpy: markers 'python_version == "3.6" and platform_system != "AIX" and platform_python_implementation != "CPython"' don't match your environment Ignoring numpy: markers 'python_version == "3.7" and platform_system != "AIX"' don't match your environment Ignoring numpy: markers 'python_version == "3.6" and platform_system == "AIX"' don't match your environment Ignoring numpy: markers 'python_version == "3.7" and platform_system == "AIX"' don't match your environment Ignoring numpy: markers 'python_version >= "3.8" and platform_system == "AIX"' don't match your environment Collecting setuptools Using cached setuptools-67.2.0-py3-none-any.whl (1.1 MB) Collecting wheel Using cached wheel-0.38.4-py3-none-any.whl (36 kB) Collecting Cython>=0.28.5 Using cached Cython-0.29.33-py2.py3-none-any.whl (987 kB) Collecting numpy==1.17.3 Using cached numpy-1.17.3.zip (6.4 MB) Preparing metadata (setup.py): started Preparing metadata (setup.py): finished with status 'done' Collecting scipy>=0.19.1 Using cached scipy-1.10.0-cp311-cp311-macosx_12_0_arm64.whl (28.7 MB) Using cached scipy-1.9.3-cp311-cp311-macosx_12_0_arm64.whl (28.4 MB) Using cached scipy-1.9.2-cp311-cp311-macosx_12_0_arm64.whl (28.4 MB) Using cached scipy-1.9.1.tar.gz (42.0 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 'error' error: subprocess-exited-with-error × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [67 lines of output] The Meson build system Version: 0.62.2 Source dir: /private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28 Build dir: /private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28/.mesonpy-7pg6lokl/build Build type: native build Project name: SciPy Project version: 1.9.1 C compiler for the host machine: cc (clang 14.0.0 "Apple clang version 14.0.0 (clang-1400.0.29.202)") C linker for the host machine: cc ld64 820.1 C++ compiler for the host machine: c++ (clang 14.0.0 "Apple clang version 14.0.0 (clang-1400.0.29.202)") C++ linker for the host machine: c++ ld64 820.1 Host machine cpu family: aarch64 Host machine cpu: arm64 Compiler for C supports arguments -Wno-unused-but-set-variable: YES Library m found: YES ../../meson.build:41:0: ERROR: Unknown compiler(s): [['gfortran'], ['flang'], ['nvfortran'], ['pgfortran'], ['ifort'], ['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 "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/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28/.mesonpy-7pg6lokl/build/meson-logs/meson-log.txt + meson setup --native-file=/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28/.mesonpy-native-file.ini -Ddebug=false -Doptimization=2 --prefix=/Library/Frameworks/Python.framework/Versions/3.11 /private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28 /private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28/.mesonpy-7pg6lokl/build Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module> main() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main json_out['return_val'] = hook(**hook_input['kwargs']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel return hook(config_settings) ^^^^^^^^^^^^^^^^^^^^^ File "/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-build-env-u2xs3lnb/overlay/lib/python3.11/site-packages/mesonpy/__init__.py", line 969, in get_requires_for_build_wheel with _project(config_settings) as project: File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 137, in __enter__ return next(self.gen) ^^^^^^^^^^^^^^ File "/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-build-env-u2xs3lnb/overlay/lib/python3.11/site-packages/mesonpy/__init__.py", line 948, in _project with Project.with_temp_working_dir( File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 137, in __enter__ return next(self.gen) ^^^^^^^^^^^^^^ File "/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-build-env-u2xs3lnb/overlay/lib/python3.11/site-packages/mesonpy/__init__.py", line 777, in with_temp_working_dir yield cls(source_dir, tmpdir, build_dir) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-build-env-u2xs3lnb/overlay/lib/python3.11/site-packages/mesonpy/__init__.py", line 682, in __init__ self._configure(reconfigure=bool(build_dir) and not native_file_mismatch) File "/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-build-env-u2xs3lnb/overlay/lib/python3.11/site-packages/mesonpy/__init__.py", line 713, in _configure self._meson( File "/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-build-env-u2xs3lnb/overlay/lib/python3.11/site-packages/mesonpy/__init__.py", line 696, in _meson return self._proc('meson', *args) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-build-env-u2xs3lnb/overlay/lib/python3.11/site-packages/mesonpy/__init__.py", line 691, in _proc subprocess.check_call(list(args)) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/subprocess.py", line 413, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['meson', 'setup', '--native-file=/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28/.mesonpy-native-file.ini', '-Ddebug=false', '-Doptimization=2', '--prefix=/Library/Frameworks/Python.framework/Versions/3.11', '/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28', '/private/var/folders/c2/v1x2pn511b79mxpbdj208p780000gn/T/pip-install-txosa8qz/scipy_734bcce5e1d549fab1cee5a52c8d7f28/.mesonpy-7pg6lokl/build']' returned non-zero exit status 1. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error × Getting requirements to build wheel 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. [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.
解决方案
核心问题分析
- Python版本不兼容:scikit-learn 0.23.1官方仅支持Python 3.6、3.7、3.8,你的Python 3.11不在支持范围内,这会导致依赖包(如numpy、scipy)版本匹配混乱。
- 缺少Fortran编译器:错误日志明确显示找不到gfortran,而scipy构建必须依赖Fortran编译器。
方法一:切换到兼容的Python版本(推荐)
用pyenv管理多Python版本,步骤如下:
- 安装pyenv:
brew install pyenv - 安装Python 3.8.x版本(比如3.8.18):
pyenv install 3.8.18 - 在当前项目目录切换Python版本:
pyenv local 3.8.18 - 验证版本:
python --version # 应显示Python 3.8.18 - 安装scikit-learn==0.23.1:
pip install scikit-learn==0.23.1
方法二:在Rosetta 2环境下安装(不换Python版本的备选)
M1芯片可以通过Rosetta 2模拟x86环境,获取兼容的预编译wheel:
- 打开终端时右键选择「使用Rosetta打开」,或者执行:
arch -x86_64 zsh - 安装gfortran(先解决编译器问题):
brew install gfortran - 安装scikit-learn==0.23.1:
pip3 install scikit-learn==0.23.1 --force-reinstall
方法三:手动指定兼容的依赖版本
如果坚持用Python 3.11,尝试手动指定兼容的numpy和scipy版本(不保证完全稳定):
- 先安装gfortran:
brew install gfortran - 依次安装依赖:
pip install numpy==1.21.6 scipy==1.7.3 pip install scikit-learn==0.23.1
内容的提问来源于stack exchange,提问作者Arturo Sbr
相关产品推荐
相关产品推荐

