Package requires a different Python: 2.7.17 not in '>=3.6.1' while setting up pre-commit

13,333

The issue was that I have both Python2.7 and 3 installed. And my pre-commit was installed was using Python 2.7 as the default.


Solution 1: remove pre-commit from Python2.7 and add it to Python3.

As per the creator of pre-commit - @anthony-sottile - it is better to use pre-commit with Python3. To do that we will have to uninstall pre-commit from Python2.7 and install it via Python3.

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

Solution 2: keeping pre-commit with Python2.7 (not recommended)

To solve this I used default_language_version from the pre-commit documentation. Refer: https://pre-commit.com/#overriding-language-version

By setting the default_language_version all hooks will use this particular version. If any particular hook needs to be overridden this property - language_version: - may be set on the hook.

Eg:-

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

This example .pre-commit-config.yaml file set the default python version to Python 3. For the hook - check-merge-conflict it will use Python 2.7.

Share:
13,333
Roopak A Nelliat
Author by

Roopak A Nelliat

A Computer Engineer from Kannur, India. I call myself a product engineer as I am good with Front-end and Backend development. I have good experience working with Data science team and good basic projects on ML/AI (Sentiment analysis :P) Current working as Software Engineer with Airbase Inc. Tried running a startup in logistics/last-mile delivery solutions. Worked as SDE-2 with Zestmoney, India. Worked as an R&D Engineer at SureWaves Media Tech Pvt. Ltd., Bengaluru.

Updated on June 25, 2022

Comments

  • Roopak A Nelliat
    Roopak A Nelliat almost 2 years

    I cloned a repository, installed pre-commit and was committing for the first time. This is the time when pre-commit packages actually get installed and setup. I faced the following issue.

    [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'