pre-commit安装isort 5.10.1失败,Python3.7兼容场景下如何解决?
解决Python3.7环境下isort pre-commit钩子安装的Poetry配置错误
问题重现
安装isort pre-commit钩子时出现如下错误:
[INFO] Installing environment for https://github.com/pycqa/isort. [INFO] Once installed this environment will be reused. [INFO] This may take a few minutes... An unexpected error has occurred: CalledProcessError: command: ('/builds/.../.cache/pre-commit/repo0_h0f938/py_env-python3.8/bin/python', '-mpip', 'install', '.') return code: 1 expected return code: 0 [...] stderr: ERROR: Command errored out with exit status 1: [...] File "/tmp/pip-build-env-_3j1398p/overlay/lib/python3.8/site-packages/poetry/core/masonry/api.py", line 40, in prepare_metadata_for_build_wheel poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False) File "/tmp/pip-build-env-_3j1398p/overlay/lib/python3.8/site-packages/poetry/core/factory.py", line 57, in create_poetry raise RuntimeError("The Poetry configuration is invalid: " + message) RuntimeError: The Poetry configuration is invalid: - [extras.pipfile_deprecated_finder.2] 'pip-shims<=0.3.4' does not match '^[a-zA-Z-_.0-9]+$'
已知升级到isort 5.12.0可解决该问题,但该版本不支持Python3.7,且暂不计划升级Python,可通过以下方案处理:
解决方案
方案1:锁定isort到兼容Python3.7的修复版本
将pre-commit配置中的isort版本指定为5.11.0,这是isort最后一个支持Python3.7且修复了该Poetry配置依赖问题的版本。修改pre-commit-config.yaml:
repos: - repo: https://github.com/pycqa/isort rev: 5.11.0 hooks: - id: isort
修改后重新执行钩子安装:
pre-commit install --install-hooks
方案2:强制指定兼容的pip-shims版本
如果需要保留当前使用的isort版本,可在钩子配置中添加额外依赖,强制锁定pip-shims到符合格式要求的版本:
repos: - repo: https://github.com/pycqa/isort rev: <你的当前isort版本号> hooks: - id: isort additional_dependencies: ["pip-shims==0.3.4"]
此方法会覆盖isort依赖中对pip-shims的错误版本约束,避免Poetry配置校验失败。
方案3:清理pre-commit缓存
若缓存的旧环境导致依赖冲突,可清理缓存后重新安装:
pre-commit clean pre-commit install --install-hooks
内容的提问来源于stack exchange,提问作者LOTEAT
相关产品推荐
相关产品推荐

