Mac M1运行yarn install报错:PATH中找不到Python可执行文件python2
Mac M1 设备yarn安装node-sass失败(找不到Python可执行文件)
环境信息
- Node版本:14.19.3
- Yarn版本:1.22.18
- 问题依赖:
node-sass@^4.14.1
报错现象
执行yarn install过程中,构建node-sass包阶段失败。node-gyp配置编译环境时,先后在系统PATH中查找python2、python可执行文件均未命中,抛出ENOENT文件不存在错误,最终提示无法找到Python可执行文件,可通过设置PYTHON环境变量指定对应路径。
完整错误日志如下:
yarn add v1.22.18 [1/5] 🔍 Resolving packages... warning node-pre-gyp@0.17.0: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future [2/5] 🚚 Fetching packages... [3/5] 🔗 Linking dependencies... warning "react-apollo > @apollo/react-components@3.1.3" has unmet peer dependency "apollo-cache@^1.3.2". warning " > react-formik-ui@3.0.8" has incorrect peer dependency "formik@^1.5.7". warning "react-formik-ui > react-phonenr-input > react-country-flag@1.0.2" has unmet peer dependency "prop-types@^15.5.4". warning " > babel-loader@8.1.0" has unmet peer dependency "webpack@>=2". warning " > redoc@2.0.0-rc.31" has incorrect peer dependency "styled-components@^4.1.1". warning "redoc > swagger2openapi > better-ajv-errors@0.6.7" has unmet peer dependency "ajv@4.11.8 - 6". warning " > styled-components@5.1.1" has unmet peer dependency "react-is@>= 16.8.0". warning " > eslint-config-airbnb@17.1.1" has incorrect peer dependency "eslint@^4.19.1 || ^5.3.0". warning "eslint-config-airbnb > eslint-config-airbnb-base@13.2.0" has incorrect peer dependency "eslint@^4.19.1 || ^5.3.0". warning " > eslint-config-react-app@5.1.0" has unmet peer dependency "babel-eslint@10.x". warning " > eslint-config-react-app@5.1.0" has unmet peer dependency "eslint-plugin-flowtype@3.x". warning " > eslint-config-react-app@5.1.0" has unmet peer dependency "eslint-plugin-react-hooks@1.x". [4/5] 🔨 Building fresh packages... [-/13] ⠐ waiting... [7/13] ⠐ node-sass [-/13] ⠐ waiting... [-/13] ⠐ waiting... error /Users/suhasr/someproject/node_modules/node-sass: Command failed. Exit code: 1 Command: node scripts/build.js Arguments: Directory: /Users/suhasr/someproject/node_modules/node-sass Output: Building: /opt/homebrew/Cellar/node@14/14.19.3/bin/node /Users/suhasr/someproject/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= gyp info it worked if it ends with ok gyp verb cli [ gyp verb cli '/opt/homebrew/Cellar/node@14/14.19.3/bin/node', gyp verb cli '/Users/suhasr/someproject/node_modules/node-gyp/bin/node-gyp.js', gyp verb cli 'rebuild', gyp verb cli '--verbose', gyp verb cli '--libsass_ext=', gyp verb cli '--libsass_cflags=', gyp verb cli '--libsass_ldflags=', gyp verb cli '--libsass_library=' gyp verb cli ] gyp info using node-gyp@3.8.0 gyp info using node@14.19.3 | darwin | arm64 gyp verb command rebuild [] gyp verb command clean [] gyp verb clean removing "build" directory gyp verb command configure [] gyp verb check python checking for Python executable "python2" in the PATH gyp verb `which` failed Error: not found: python2 gyp verb `which` failed at getNotFoundError (/Users/suhasr/someproject/node_modules/which/which.js:13:12) gyp verb `which` failed at F (/Users/suhasr/someproject/node_modules/which/which.js:68:19) gyp verb `which` failed at E (/Users/suhasr/someproject/node_modules/which/which.js:80:29) gyp verb `which` failed at /Users/suhasr/someproject/node_modules/which/which.js:89:16 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/index.js:42:5 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/mode.js:8:5 gyp verb `which` failed at FSReqCallback.oncomplete (fs.js:192:21) gyp verb `which` failed python2 Error: not found: python2 gyp verb `which` failed at getNotFoundError (/Users/suhasr/someproject/node_modules/which/which.js:13:12) gyp verb `which` failed at F (/Users/suhasr/someproject/node_modules/which/which.js:68:19) gyp verb `which` failed at E (/Users/suhasr/someproject/node_modules/which/which.js:80:29) gyp verb `which` failed at /Users/suhasr/someproject/node_modules/which/which.js:89:16 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/index.js:42:5 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/mode.js:8:5 gyp verb `which` failed at FSReqCallback.oncomplete (fs.js:192:21) { gyp verb `which` failed code: 'ENOENT' gyp verb `which` failed } gyp verb check python checking for Python executable "python" in the PATH gyp verb `which` failed Error: not found: python gyp verb `which` failed at getNotFoundError (/Users/suhasr/someproject/node_modules/which/which.js:13:12) gyp verb `which` failed at F (/Users/suhasr/someproject/node_modules/which/which.js:68:19) gyp verb `which` failed at E (/Users/suhasr/someproject/node_modules/which/which.js:80:29) gyp verb `which` failed at /Users/suhasr/someproject/node_modules/which/which.js:89:16 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/index.js:42:5 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/mode.js:8:5 gyp verb `which` failed at FSReqCallback.oncomplete (fs.js:192:21) gyp verb `which` failed python Error: not found: python gyp verb `which` failed at getNotFoundError (/Users/suhasr/someproject/node_modules/which/which.js:13:12) gyp verb `which` failed at F (/Users/suhasr/someproject/node_modules/which/which.js:68:19) gyp verb `which` failed at E (/Users/suhasr/someproject/node_modules/which/which.js:80:29) gyp verb `which` failed at /Users/suhasr/someproject/node_modules/which/which.js:89:16 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/index.js:42:5 gyp verb `which` failed at /Users/suhasr/someproject/node_modules/isexe/mode.js:8:5 gyp verb `which` failed at FSReqCallback.oncomplete (fs.js:192:21) { gyp verb `which` failed code: 'ENOENT' gyp verb `which` failed } gyp ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable. gyp ERR! stack at PythonFinder.failNoPython (/Usersnode_modules/node-gyp/lib/configure.js:484:19) gyp ERR! stack at PythonFinder.<anonymous> (/Users/node_modules/node-gyp/lib/configure.js:406:16) gyp ERR! stack at F (/Users/node_modules/which/which.js:68:16) gyp ERR! stack at E (/Users/node_modules/which/which.js:80:29) gyp ERR! stack at /Users/node_modules/which/which.js:89:16 gyp ERR! stack at /Users/node_modules/isexe/index.js:42:5 gyp ERR! stack at /Usersnode_modules/isexe/mode.js:8:5 gyp ERR! stack at FSReqCallback.oncomplete (fs.js:192:21) gyp ERR! System Darwin 21.5.0 gyp ERR! command "/opt/homebrew/Cellar/node@14/14.19.3/bin/node" "/Usersnode_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! cwd /Users/node_modules/node-sass gyp ERR! node -v v14.19.3
报错根因
node-sass@4.x依赖的node-gyp@3.x版本仅支持Python 2.x编译环境,Mac M1系列芯片默认系统不再预装Python 2,且默认无python全局命令,直接触发找不到可执行文件的报错。node-sass@4.14.1官方未提供arm64架构的预编译二进制包,即使补齐Python依赖,仍可能存在架构兼容导致的编译失败问题。
解决方案
方案1(优先推荐,无编译依赖)
直接替换node-sass为官方推荐的dart-sass(包名为sass),语法100%兼容原有scss代码,无需本地编译,完全适配M1系列arm64架构,不存在Python、C++编译环境依赖问题:
- 移除原有node-sass依赖:
yarn remove node-sass - 安装sass依赖:
yarn add sass -D - 清理缓存后重新安装全部依赖即可:
rm -rf node_modules yarn.lock yarn install
方案2(必须保留node-sass@4.x版本时使用)
手动补齐编译依赖,适配M1架构:
- 安装pyenv管理Python版本,安装Python 2.7.18作为编译依赖:
brew install pyenv pyenv install 2.7.18 pyenv global 2.7.18 # zsh用户执行以下命令配置环境变量,bash用户替换为~/.bashrc即可 echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init -)"' >> ~/.zshrc source ~/.zshrc - 安装Xcode命令行编译工具:
xcode-select --install - 配置yarn和npm指定Python路径:
npm config set python $(which python2) yarn config set python $(which python2) - 清理原有依赖缓存后重新安装:
rm -rf node_modules yarn.lock yarn install
临时快速修复(仅解决Python命令缺失问题)
如果系统已安装Python3,可临时创建软链接让python命令指向Python3,注意该方案可能因Python版本不兼容导致后续编译失败:
sudo ln -s $(which python3) /usr/local/bin/python
内容的提问来源于stack exchange,提问作者Suhas R
相关产品推荐
相关产品推荐

