描述。
在尝试使用预提交挂钩时,我遇到了一些困难,包括Conda环境中的格式化 pip
包。
米维
使用以下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
文件)?
发布于 2022-05-09 21:25:25
不幸的是,这在conda
中是一个已知的错误--尽管我不熟悉它的状态。他们错误地将一个python3.1
二进制文件作为默认的可执行文件(它应该被称为python3.10
--它们有一个可能是sys.version[:3]
的2020年问题)
幸运的是,您可以指示预提交忽略它的“默认”版本检测(即检测您的python3.1
可执行文件),并给它一个要使用的python版本的特定指令。
要做到这一点有两种方法:
default_language_version
:作为“默认”,在未设置language_version
时应用default_language_version:
python: python3.10 # or python3
# ...
language_version
(以覆盖钩子提供程序设置的默认值)# ...
- id: black
language_version: python3.10
免责声明:我创建了预提交。
发布于 2022-10-19 12:06:13
关于预提交存储库https://github.com/pre-commit/pre-commit/issues/1375中的观察,出现了一个滴答的打开。
一个预提交贡献者建议在language_version: python3
中使用.pre-commit-config.yaml
中的黑色配置。
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
https://stackoverflow.com/questions/72177222
复制相似问题