Search code examples
pre-commitpre-commit.com

Why does pre-commit fail with "failed to find interpreter for Builtin discover of python_spec='python3.10'"?


I've just started using pre-commit in a project I've been working on for a few weeks. I defined my .pre-commit-config.yaml

---
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: check-json
      - id: check-merge-conflict
      - id: end-of-file-fixer
      - id: check-yaml
      - id: trailing-whitespace

  - repo: https://github.com/ambv/black
    rev: 23.11.0
    hooks:
      - id: black
        language_version: python3.10

  - repo: https://github.com/astral-sh/ruff-pre-commit
    # Ruff version.
    rev: v0.1.6
    hooks:
      # Run the linter.
      - id: ruff
      # Run the formatter.
      - id: ruff-format

  - repo: https://github.com/igorshubovych/markdownlint-cli
    rev: v0.37.0
    hooks:
      - id: markdownlint
        args: ["--config", ".github/linters/.markdownlint.json"]

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.7.0
    hooks:
      - id: mypy
        name: mypy tests
        files: ^tests/

When I run pre-commit it fails with this error:

[INFO] Installing environment for https://github.com/ambv/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/usr/local/Cellar/pre-commit/3.4.0/libexec/bin/python', '-mvirtualenv', '/Users/redacted/.cache/pre-commit/repoljj12k4y/py_env-python3.10', '-p', 'python3.10')
return code: 1
stdout:
RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.10'
stderr: (none)
Check the log at /Users/jamiethomson/.cache/pre-commit/pre-commit.log

I simply don't understand why this is happening. I think its trying to create a virtualenv at /Users/redacted/.cache/pre-commit/repoljj12k4y/py_env-python3.10, but I don't know why. I can confirm that that directory does not exist:

> ls /Users/redacted/.cache/pre-commit/repoljj12k4y/py_env-python3.10                                                   
"/Users/redacted/.cache/pre-commit/repoljj12k4y/py_env-python3.10": No such file or directory (os error 2)

I'm on a mac by the way, and I use pyenv to manage python versions, in case that matters.

I assume that directory location is stored in some config somewhere but I've looked for such a thing and I can't find it. Does anyone know why pre-commit would be trying to create a virtualenv at this location?


Solution

  • pre-commit installs tools in isolated environments to avoid interfering with user space, to avoid conflicting tools, and to ensure consistent installation of exactly what's requested

    in this case you've requested language_version: python3.10 and so pre-commit will attempt to create an environment using python3.10

    the error message comes from virtualenv and indicates that you do not have python 3.10 available

    the directory does not exist after your execution because the installation is cleaned up on failure


    disclaimer: I wrote pre-commit