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

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

说明。

在尝试使用预提交挂钩时,我遇到了一些困难,包括 the latest Lava-nc release by Intel in .tar.gz format pip 在 Conda 环境中打包。

MWE

使用以下 Conda environment.yaml 文件:

# 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

如果我包含以下 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留言

git commit "something"pre-commit run --all-files return 两者:

[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

错误日志。

pre-commit.log的输出是:

### 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)

解决方法

如果我删除 /home/name/anaconda3/envs/networkx-to-lava/bin/python3.1 文件,则可以避免此错误。但是,这会破坏自动化并且似乎是一种不合适的解决方法。

问题

如何确保创建和激活 Conda 环境允许直接 运行 预提交挂钩而不会出错(无需删除 python3.1 文件)?

不幸的是,这是 conda 中的一个已知错误——尽管我不熟悉它的状态。他们错误地将 python3.1 二进制文件作为默认可执行文件(它应该被称为 python3.10 -- 他们有一个 2020 问题,可能是 sys.version[:3] 某处)

幸运的是,您可以指示 pre-commit 忽略它的“默认”版本检测(它正在检测您的 python3.1 可执行文件)并给它一个特定的指令,说明要使用哪个 python 版本使用

有两种方法:

  1. default_language_version:作为“默认值”,在未设置 language_version 时应用
default_language_version:
    python: python3.10  # or python3

# ...
  1. language_version 在特定的钩子上(覆盖钩子提供者设置的任何默认值)
# ...

    -   id: black
        language_version: python3.10

免责声明:我创建了 pre-commit