Skip to content

pre-commit fails to initialize due to python 3.8 dependency #12868

@soxofaan

Description

@soxofaan

I wrestled with this pre-commit issue when trying to contribute a PR yesterday and could not find pointers in the issues or discussions, so I'm at least dumping my findings here. To be discussed if/how this has to be documented properly somewhere.

With #12747 (end of August) the pre-commit run explicitly depends on python 3.8:

- id: mypy
files: ^(src/|testing/|scripts/)
args: []
language_version: "3.8"

(python 3.8 is EOL since this week, but that's not the actual problem here, I would have had the same issue with 3.9)

Running pre-commit failed on me during pre-commit update, e.g.:

$ pre-commit run
...
[INFO] Installing environment for https://github.com/pre-commit/mirrors-mypy.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/home/johndoe/.local/pipx/venvs/pre-commit/bin/python', '-mvirtualenv', '/home/johndoe/.cache/pre-commit/repodljwkw2k/py_env-3.8', '-p', '3.8')
return code: 1
stdout:
    RuntimeError: failed to find interpreter for Builtin discover of python_spec='3.8'
stderr: (none)
Check the log at /home/johndoe/.cache/pre-commit/pre-commit.log

After some rabbit hole crawling I figured out that I have to make sure that virtualenv (used by pre-commit) is able to resolve "3.8" to an actual interpreter on my system.

from https://virtualenv.pypa.io/en/latest/user_guide.html#python-discovery:

Given the specifier virtualenv will apply the following strategy to discover/find the system executable:
...

  • Try to discover a matching python executable within the folders enumerated on the PATH environment variable. In this case we’ll try to find an executable that has a name roughly similar to the specification (for exact logic, please see the implementation code).

I'm using asdf (which uses pyenv under the hood) and luckily had still a 3.8 version around. I found the direct (non-shimmed) path to the executable with

$ (asdf shell python 3.8.15; python3.8 -c "import sys;print(sys.executable)")
/home/johndoe/.asdf/installs/python/3.8.15/bin/python3.8

And appended the containing folder to my PATH when triggering the pre-commit update:

$ export PATH=$PATH:/home/johndoe/.asdf/installs/python/3.8.15/bin/
$ pre-commit run

pre-commit then could successfully update/install the hooks, and I could commit again

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions