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

CoreUI Vue项目node-gyp与Python3.11兼容问题求助

解决node-sass安装报错:Python语法不兼容问题

报错信息

check python checking for Python executable "python3" in the PATH
gyp verb `which` succeeded python3 /usr/bin/python3
gyp ERR! configure error 
gyp ERR! stack Error: Command failed: /usr/bin/python3 -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
gyp ERR! stack 
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:383:12)
gyp ERR! stack     at ChildProcess.emit (events.js:400:28)
gyp ERR! stack     at maybeClose (internal/child_process.js:1088:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:296:5)
gyp ERR! System Linux 6.1.0-21-amd64
gyp ERR! command "/root/.nvm/versions/node/v14.21.3/bin/node" "/home/web3/web/.../node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /home/web3/web/.../node_modules/node-sass
gyp ERR! node -v v14.21.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
Build failed with error code: 1

问题原因

你当前使用的node-gyp v3.8.0是旧版本,仅支持Python2的print语法(无括号),但系统里的Python3.11强制要求print()必须加括号,导致语法报错。同时项目依赖的node-sass ~4.13.1自带的就是这个旧版node-gyp。

解决思路

方案一:降级Python到兼容版本

node-gyp v3.8.0支持Python3.6~3.9,可通过版本管理工具切换:

  • 安装pyenv管理Python版本:
    curl https://pyenv.run | bash
    
  • 重启终端后安装Python3.9:
    pyenv install 3.9.17
    
  • 在项目目录设置本地Python版本:
    pyenv local 3.9.17
    
  • 清理后重新安装依赖:
    rm -rf node_modules package-lock.json
    npm install
    

方案二:升级node-gyp版本

手动指定使用支持Python3.11的新版node-gyp:

  • 全局安装最新版node-gyp:
    npm install -g node-gyp@latest
    
  • 配置npm使用全局的node-gyp:
    npm config set node_gyp "/root/.nvm/versions/node/v14.21.3/lib/node_modules/node-gyp/bin/node-gyp.js"
    
  • 清理后重新安装:
    rm -rf node_modules package-lock.json
    npm install
    

方案三:替换node-sass为dart-sass(长期推荐)

node-sass已停止维护,dart-sass是官方替代,兼容性更好,无需依赖旧版node-gyp:

  1. 卸载旧依赖:
    npm uninstall node-sass sass-loader
    
  2. 安装适配Vue2的dart-sass和sass-loader:
    npm install sass@^1.60.0 sass-loader@^10.1.1 --save-dev
    
  3. 若项目有vue.config.js,添加配置指定sass实现:
    module.exports = {
      css: {
        loaderOptions: {
          sass: {
            implementation: require('sass')
          }
        }
      }
    }
    
  4. 清理后重新安装:
    rm -rf node_modules package-lock.json
    npm install
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 20:24:52