Pre-commit钩子安装hatchling依赖报错,寻求解决方案
问题描述
我正在为Flask仓库配置Git预提交钩子,用来自动格式化Python代码。按照pre-commit的指引,已经把pre-commit添加到requirements.txt,并编写了pre-commit-config.yaml配置文件:
repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v2.3.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/psf/black rev: 22.12.0 hooks: - id: black
执行pip install pre-commit和pre-commit install完成钩子安装后,执行git commit时触发报错,错误核心是找不到满足hatchling>=1.8.0的版本,完整报错信息如下:
[INFO] Installing environment for https://github.com/psf/black. [INFO] Once installed this environment will be reused. [INFO] This may take a few minutes... An unexpected error has occurred: CalledProcessError: command: ('/Users/vcui/.cache/pre-commit/repoq7qnm1w0/py_env-python3.6/bin/python', '-mpip', 'install', '.') return code: 1 expected return code: 0 stdout: Processing /Users/vcui/.cache/pre-commit/repoq7qnm1w0 Installing build dependencies: started Installing build dependencies: finished with status 'error' stderr: ERROR: Command errored out with exit status 1: command: /Users/vcui/.cache/pre-commit/repoq7qnm1w0/py_env-python3.6/bin/python /Users/vcui/.cache/pre-commit/repoq7qnm1w0/py_env-python3.6/lib/python3.6/site-packages/pip install --ignore-installed --no-user --prefix /private/var/folders/9r/jcsx6jv923lb_fmm4w09zq5m0000gp/T/pip-build-env-wtkwct01/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'hatchling>=1.8.0' hatch-vcs hatch-fancy-pypi-readme cwd: None Complete output (2 lines): ERROR: Could not find a version that satisfies the requirement hatchling>=1.8.0 (from versions: 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.10.0, 0.11.0, 0.11.1, 0.11.2, 0.11.3, 0.12.0, 0.13.0, 0.14.0, 0.15.0, 0.16.0, 0.17.0, 0.18.0, 0.19.0, 0.20.0, 0.20.1, 0.21.0, 0.21.1, 0.22.0, 0.23.0, 0.24.0, 0.25.0, 0.25.1) ERROR: No matching distribution found for hatchling>=1.8.0
解决方案
问题根源
Python 3.6已停止官方维护,hatchling从1.0版本开始不再支持Python 3.6,而你配置的black 22.12.0构建时需要hatchling>=1.8.0,两者版本不兼容导致安装失败。
方法一:升级Python版本(推荐)
将Python版本升级到3.7及以上(black 22.12.0支持Python 3.7+),升级后pip可以正常安装符合要求的hatchling版本。操作步骤:
- 安装Python 3.7+版本
- 更新项目的虚拟环境,指向新的Python版本
- 重新执行
pre-commit install和git commit
方法二:降级black到兼容Python 3.6的版本
如果暂时无法升级Python,修改pre-commit-config.yaml中black的版本为最后支持Python 3.6的版本21.12b0:
repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v2.3.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/psf/black rev: 21.12b0 hooks: - id: black
然后执行以下命令清理旧环境并重新触发钩子:
pre-commit clean git commit
内容的提问来源于stack exchange,提问作者Victor Cui
相关产品推荐
相关产品推荐

