【问题标题】:Package requires a different Python: 2.7.17 not in '>=3.6.1' while setting up pre-commit包需要不同的 Python:2.7.17 not in '>=3.6.1' while setting up pre-commit
【发布时间】:2020-09-03 21:08:04
【问题描述】:

我克隆了一个存储库,安装了 pre-commit 并且是第一次提交。 这是实际安装和设置预提交包的时间。我遇到了以下问题。

[INFO] Installing environment for https://github.com/asottile/seed-isort-config.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/home/roopak/.cache/pre-commit/repokb2ckm/py_env-python2.7/bin/python', u'/home/roopak/.cache/pre-commit/repokb2ckm/py_env-python2.7/bin/pip', 'install', '.')
return code: 1
expected return code: 0
stdout:
    Processing /home/roopak/.cache/pre-commit/repokb2ckm

stderr:
    DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
    ERROR: Package 'seed-isort-config' requires a different Python: 2.7.17 not in '>=3.6.1'

【问题讨论】:

    标签: python git pre-commit-hook pre-commit pre-commit.com


    【解决方案1】:

    问题是我同时安装了 Python2.7 和 3。我的pre-commit 安装时默认使用 Python 2.7。


    解决方案一:从 Python2.7 中移除 pre-commit 并将其添加到 Python3。

    根据 pre-commit 的创建者 - @anthony-sottile - 最好在 Python3 中使用 pre-commit。为此,我们必须从 Python2.7 卸载 pre-commit 并通过 Python3 安装它。

    $ pip uninstall pre-commit  # uninstall from Python2.7
    $ pip3 install pre-commit # install with Python3
    

    解决方案 2:使用 Python2.7 保持预提交(不推荐)

    为了解决这个问题,我使用了预提交文档中的default_language_version。 参考:https://pre-commit.com/#overriding-language-version

    通过设置default_language_version,所有钩子都将使用这个特定版本。如果需要覆盖任何特定的挂钩,则可以在挂钩上设置此属性 - language_version:

    例如:-

    default_language_version:
        # force all unspecified python hooks to run python3
        python: python3
    repos:
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v2.5.0
        hooks:
          - id: trailing-whitespace
            name: trim trailing whitespace
            description: This hook trims trailing whitespace on files
            entry: trailing-whitespace-fixer
    
          - id: check-merge-conflict
            name: check for merge conflict
            description: Prevent accidentally commiting files with merge conflicts.
            language_version:
                python: python2.7
    
    

    此示例.pre-commit-config.yaml 文件将默认 python 版本设置为 Python 3。对于钩子 - check-merge-conflict,它将使用 Python 2.7。

    【讨论】:

    • 请注意,将 pre-commit 安装到 python2 意味着您将永远停留在旧版本上(
    猜你喜欢
    • 2022-12-27
    • 2019-11-26
    • 2020-06-02
    • 2011-05-18
    • 2022-12-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多