condapre-commit-hookpre-commitpre-commit.com

RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.1'


Description.

While trying to use pre-commit hooks, I am experiencing some difficulties, including the latest Lava-nc release by Intel in .tar.gz format pip package in a Conda environment.

MWE

The following Conda environment.yaml file is used:

# This file is to automatically configure your environment. It allows you to
# run the code with a single command without having to install anything
# (extra).

# First run:: conda env create --file environment.yml
# If you change this file, run: conda env update --file environment.yml

# Instructions for this networkx-to-lava-nc repository only. First time usage
# On Ubuntu (this is needed for lava-nc):
# sudo apt upgrade
# sudo apt full-upgrade
# yes | sudo apt install gcc

# Conda configuration settings. (Specify which modules/packages are installed.)
name: networkx-to-lava
channels:
  - conda-forge
  - conda
dependencies:
- anaconda
- conda:
# Run python tests.
  - pytest-cov
- pip
- pip:
# Run pip install on .tar.gz file in GitHub repository (For lava-nc only).
  - https://github.com/lava-nc/lava/releases/download/v0.3.0/lava-nc-0.3.0.tar.gz
# Auto check static typing.
  - mypy
# Auto check programming style aspects.
  - pylint

If I include the following MWE .pre-commit-config.yaml:

repos:
# Test if the variable typing is correct
# - repo: https://github.com/python/mypy
 - repo: https://github.com/pre-commit/mirrors-mypy
   rev: v0.950
   hooks:
    - id: mypy

Output/Error Message

The git commit "something" and the pre-commit run --all-files return both:

[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/name/anaconda3/envs/networkx-to-lava/bin/python3.1', '-mvirtualenv', '/home/name/.cache/pre-commit/repolk_aet_q/py_env-python3.1', '-p', 'python3.1')
return code: 1
expected return code: 0
stdout:
    RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.1'
    
stderr: (none)
Check the log at /home/name/.cache/pre-commit/pre-commit.log

Error Log.

The output of pre-commit.log is:

### version information


pre-commit version: 2.19.0
git --version: git version 2.30.2
sys.version:
    3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:39:04) [GCC 10.3.0]
sys.executable: /home/name/anaconda3/envs/networkx-to-lava/bin/python3.1
os.name: posix
sys.platform: linux


### error information


An unexpected error has occurred: CalledProcessError: command: ('/home/name/anaconda3/envs/networkx-to-lava/bin/python3.1', '-mvirtualenv', '/home/name/.cache/pre-commit/repolk_aet_q/py_env-python3.1', '-p', 'python3.1')
return code: 1
expected return code: 0
stdout:
    RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.1'
    
stderr: (none)

Traceback (most recent call last):
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/error_handler.py", line 73, in error_handler
    yield
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/main.py", line 361, in main
    return hook_impl(
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/commands/hook_impl.py", line 238, in hook_impl
    return retv | run(config, store, ns)
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/commands/run.py", line 414, in run
    install_hook_envs(to_install, store)
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/repository.py", line 223, in install_hook_envs
    _hook_install(hook)
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/repository.py", line 79, in _hook_install
    lang.install_environment(
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/languages/python.py", line 219, in install_environment
    cmd_output_b(*venv_cmd, cwd='/')
  File "/home/name/anaconda3/envs/networkx-to-lava/lib/python3.10/site-packages/pre_commit/util.py", line 146, in cmd_output_b
    raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
pre_commit.util.CalledProcessError: command: ('/home/name/anaconda3/envs/networkx-to-lava/bin/python3.1', '-mvirtualenv', '/home/name/.cache/pre-commit/repolk_aet_q/py_env-python3.1', '-p', 'python3.1')
return code: 1
expected return code: 0
stdout:
    RuntimeError: failed to find interpreter for Builtin discover of python_spec='python3.1'
    
stderr: (none)

Workaround

This error is evaded if I remove the /home/name/anaconda3/envs/networkx-to-lava/bin/python3.1 file. However, that breaks automation and seems to be an improper work-around.

Question

How can I ensure creating and activating the Conda environment allows direct running of the pre-commit hook without error (without having to delete the python3.1 file)?


Solution

  • this is unfortunately a known bug in conda -- though I'm unfamiliar with the status on it. they're mistakenly shipping a python3.1 binary as the default executable (it should be called python3.10 -- they have a 2020 problem that's probably a sys.version[:3] somewhere)

    fortunately, you can instruct pre-commit to ignore its "default" version detection (which is detecting your python3.1 executable) and give it a specific instruction on which python version to use

    there's two approaches to that:

    1. default_language_version: as a "default" which applies when language_version is not set
    default_language_version:
        python: python3.10  # or python3
    
    # ...
    
    1. language_version on the specific hook (to override whatever default value the hook provider has set)
    # ...
    
        -   id: black
            language_version: python3.10
    

    disclaimer: I created pre-commit