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

VSCode提交UI触发pre-commit与black钩子版本不兼容错误求助

解决VS Code UI提交时pre-commit版本不匹配问题

问题现象

在WSL(Ubuntu)的Python虚拟环境中安装了pre-commit 2.20.0,通过WSL终端执行pre-commit run --all-files或直接git commit命令时,black等所有钩子都能正常运行;但使用VS Code的提交按钮或按下Ctrl+Enter提交时,弹出版本不匹配错误:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[ERROR] The hook `black` requires pre-commit version 2.9.2 but version 2.6.0 is installed.  Perhaps run `pip install --upgrade pre-commit`.

如果从.pre-commit-config.yaml中移除black钩子,VS Code的UI提交操作就恢复正常。

版本信息

  • 系统:Win11 -> WSL(Ubuntu)
  • VS Code版本:1.72.2
  • Python版本:3.9.5
  • pre-commit版本(WSL虚拟环境):v2.20.0

配置文件(.pre-commit-config.yaml)

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.3.0
    hooks:
    -   id: check-yaml
    -   id: end-of-file-fixer
    -   id: trailing-whitespace
-   repo: https://github.com/psf/black
    rev: 22.10.0
    hooks:
    -   id: black

问题原因

VS Code默认调用的是Windows系统环境下的git和pre-commit(版本为2.6.0),而非WSL虚拟环境中安装的新版pre-commit,导致版本不满足black钩子的最低要求。

解决步骤

  1. 配置VS Code使用WSL的git

    • 打开VS Code设置(快捷键Ctrl+,),搜索git.path
    • 将路径设置为WSL中git的实际路径,例如\\wsl$\Ubuntu\usr\bin\git,可根据你的WSL发行版调整名称
    • 建议在工作区设置中配置,避免影响其他项目
  2. 指定VS Code使用WSL虚拟环境的Python解释器

    • 点击VS Code左下角显示的Python版本号,在弹出的列表中选择WSL虚拟环境对应的解释器(格式类似Python 3.9.5 ('venv': venv) [WSL: Ubuntu])
  3. 验证设置并重启

    • 重启VS Code后,使用UI提交功能测试是否还报错
    • 也可在VS Code终端执行which pre-commit,确认输出路径为WSL虚拟环境内的路径(例如/home/xxx/venv/bin/pre-commit)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 14:10:46