【问题标题】: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\'
【发布时间】:2022-10-19 20:06:13
【问题描述】:

描述。

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

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

输出/错误信息

git commit "something"pre-commit run --all-files 都返回:

[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 pre-commit-hook pre-commit pre-commit.com


    【解决方案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
      

      免责声明:我创建了预提交

    【讨论】:

    • 这绝对是固定的 - 升级 Conda。
    • 奇妙!感谢您的详细回答,我验证了解决方案 1. 有效。附言。我认为pre-commit 很棒,我只是在配置它并设置.pre-commit-config.yaml。我已经很喜欢它了,我认为这是一个很棒的概念!感谢您的建议。
    【解决方案2】:

    在预提交存储库https://github.com/pre-commit/pre-commit/issues/1375 中有一个关于观察的勾选打开

    一位预提交贡献者建议在.pre-commit-config.yaml 中使用language_version: python3 进行黑色配置

    repo: https://github.com/psf/black
        rev: 22.3.0
        hooks:
          - id: black
            language_version: python3
    

    https://github.com/pre-commit/pre-commit/issues/1375#issuecomment-603906811

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 2021-01-20
      • 2017-06-24
      • 2013-08-21
      • 2023-03-28
      • 2017-03-31
      相关资源
      最近更新 更多