无法修改node-gyp默认Python版本?指定路径后仍调用Python3报错
node-gyp rebuild 配置不生效、缓存目录报错的解决方法
执行node-gyp rebuild命令时出现如下错误:
gyp info it worked if it ends with ok gyp info using node-gyp@7.1.2 gyp info using node@9.6.1 | linux | x64 gyp info find Python using Python version 3.8.5 found at "/usr/bin/python3" gyp WARN install got an error, rolling back install gyp ERR! configure error gyp ERR! stack Error: ENOENT: no such file or directory, mkdir '/home/rando/.cache/node-gyp/9.6.1' gyp ERR! System Linux 5.8.0-45-generic gyp ERR! command "/home/rando/.nvm/versions/node/v9.6.1/bin/node" "/home/rando/.nvm/versions/node/v9.6.1/bin/node-gyp" "rebuild" "--python=/home/rando/.pyenv/versions/2.7.18/bin" gyp ERR! cwd /home/rando/Desktop/git/master-node/node/browser_engines/src/browsers/webkit gyp ERR! node -v v9.6.1 gyp ERR! node-gyp -v v7.1.2 gyp ERR! not ok
已尝试调整Python配置的操作:
- 执行命令:
node-gyp rebuild --python=/home/rando/.pyenv/versions/2.7.18/bin - 执行命令:
node-gyp rebuild --python=2.7 - 执行命令:
node-gyp rebuild --python=2.7.18
所有操作均返回相同错误,node-gyp未使用传入的Python配置,仍调用系统默认的Python3。
当前环境详情:
- 操作系统:Ubuntu 20.04
- 可用Python版本:2.7.18
- Node版本:v9.6.1
问题解决步骤
- 修正Python参数的传入格式
此前的参数传入存在路径错误:node-gyp要求--python参数必须指向Python可执行文件的完整绝对路径,不能只传Python安装的bin文件夹路径,也不能只传版本号。正确的传参格式为:node-gyp rebuild --python=/home/rando/.pyenv/versions/2.7.18/bin/python2.7 - 补全缺失的node-gyp缓存目录
报错提示/home/rando/.cache/node-gyp/9.6.1目录不存在,可手动创建该目录解决写入失败问题,执行命令:mkdir -p ~/.cache/node-gyp/9.6.1 - 全局固定node-gyp的Python配置,避免单次传参失效
通过npm全局配置永久指定Python路径,不需要每次执行命令都手动传参,执行命令:npm config set python /home/rando/.pyenv/versions/2.7.18/bin/python2.7 - 匹配Node版本兼容的node-gyp
node-gyp@7.1.2官方适配范围不包含Node v9.6.1,版本不兼容也会导致配置读取失败。如果上述步骤操作后仍报错,可将node-gyp降级到适配Node 9的版本,执行命令:npm install -g node-gyp@3.8.0
内容的提问来源于stack exchange,提问作者Rando Shtishi
相关产品推荐
相关产品推荐

