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

MSYS2中pre-commit版本不匹配问题求助

在MSYS2中运行pre-commit时的虚拟环境版本不匹配问题解决

问题场景

通过pacman -S mingw-w64-x86_64-python-pre-commit在MSYS2中成功安装pre-commit 3.7.0后,在包含hooks的仓库执行pre-commit run时出现以下错误:

$ pre-commit run
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: AssertionError: BUG: expected environment for python to be healthy immediately after install, please open an issue describing your environment

more info:

virtualenv python version did not match created version:
- actual version: <<error retrieving version from C:/Users/XYZ/.cache/pre-commit/repogzwwsprl/py_env-python3.11/Scripts/python.exe>>
- expected version: 3.11.9.final.0

Check the log at C:/Users/XYZ/.cache/pre-commit/pre-commit.log

环境版本信息

pre-commit version: 3.7.0
git --version: git version 2.45.1
sys.version:
    3.11.9 (main, Apr 12 2024, 09:55:31)  [GCC 13.2.0 64 bit (AMD64)]
sys.executable: C:/msys64/mingw64/bin/python.exe
os.name: nt
sys.platform: win32

核心问题:指定路径中不存在py_env-python3.11目录,且pre-commit无法获取虚拟环境中Python的版本。

解决步骤

  • 清理pre-commit缓存
    执行命令强制删除所有缓存的虚拟环境和仓库,让pre-commit重新创建环境:

    pre-commit clean
    

    若命令无效,手动删除缓存目录:

    rm -rf ~/.cache/pre-commit
    
  • 修复MSYS2 Python环境
    确认Python和pip功能正常:

    python --version
    pip --version
    

    重新安装相关包以修复可能的损坏:

    pacman -S --reinstall mingw-w64-x86_64-python mingw-w64-x86_64-python-pip mingw-w64-x86_64-python-pre-commit
    
  • 指定pre-commit使用的Python解释器
    在仓库的.pre-commit-config.yaml中添加配置,强制使用MSYS2的Python版本:

    default_language_version:
      python: python3.11
    

    或直接在运行时指定Python路径:

    pre-commit run --python C:/msys64/mingw64/bin/python.exe
    
  • 检查文件系统权限
    确保MSYS2终端拥有访问C:/Users/XYZ/.cache/pre-commit目录的权限,避免因权限不足导致虚拟环境目录无法创建。可尝试以管理员身份启动MSYS2终端后再执行pre-commit命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 13:02:26