完美解决stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.

今天在运行公司Vue项目时,安装运行依赖的过程中出现几种报错,在百度和博客上搜索了各种答案终于解决,于是发一篇文章来写一下我解决问题的过程。(我是在IDEA上运行Vue的,如果是vscode的话请忽略第一张和最后一张图)

下图是我安装依赖的node的版本,此时是16.18.1版本。

运行时发生了如下错误:

npm ERR! gyp verb check python checking for Python executable "python2" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python2
......
npm ERR! gyp verb `which` failed  python2 Error: not found: python2
......
npm ERR! gyp verb check python checking for Python executable "python" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python
......
npm ERR! gyp verb `which` failed  python Error: not found: python
......
npm ERR! gyp verb could not find "python". checking python launcher
npm ERR! gyp verb could not find "python". guessing location
npm ERR! gyp verb ensuring that file exists: C:\Python27\python.exe
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
......

......
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.14.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.14.1 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

分析:
上述报错信息其实有两部分,
第一部分是stack Error: Can’t find Python executable “python”, you can set the PYTHON env variable.
第二部分是Failed at the node-sass@4.14.1 postinstall script.

一步一步来,先解决第一部分:
错误提示的意思是说我没有python,我电脑里确实没有下载python,但实际上不用下载python也能解决这个问题。我查看了package.json文件中的node-sass的版本,并搜索了NodeJS和node-sass的对应版本,发现是我NodeJS的版本太高了(其实报错信息中也有提及到node的版本问题)。

不知道版本关系的可以参考下图。

但是不要立马卸载node重新安装!!!可以使用nvm管理工具,切换不同node的版本来使用,下面是github官网上下载nvm的路径,下载安装即可。

前往官网下载nvm

我重新下载的是12版本的node,使用命令nvm install 12.13.0
切换node版本,使用命令nvm use 12.13.0
然后查看当前版本,是12.13.0,切换完成。

下图是nvm部分命令的使用效果:

下图是切换后的node版本。

切换node版本后,第一部分问题已经解决,下面开始解决第二部分:
第二部分的意思是node-sass4.14.1的脚本安装失败,其实原因在第一部分也有提及,就是版本问题,NodeJS和node-sass的版本需要匹配才能成功安装,如果已经把Node改为12版本,那么问题就非常好解决了,只需要在Terminal控制台输入指令npm config set sass_binary_site=https://npm.taobao.org/mirrors/node-sass,控制台没有任何报错就说明运行成功了。

成功后继续下载依赖,没有报错,问题完美解决!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年6月11日
下一篇 2023年6月11日

相关推荐