我正在尝试在 Ubuntu 22.04.1 / Python 3.10.6 上使用 pre-commit 2.20.0。
我按照 https://pre-commit.com/#installation 上的说明进行操作,并使用
pre-commit
(22.2.2) 安装了 pip
。
这是我的
.pre-commit-config.yaml
文件:
$ cat /home/username/Projects/web/app/.pre-commit-config.yaml
default_language_version:
python: python3.10
repos:
- repo: https://github.com/pycqa/isort
rev: "5.10.1"
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/psf/black
rev: "22.8.0"
hooks:
- id: black
language_version: python3.10
但是我在提交新文件时遇到错误:
$ git commit -m "Update file."
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('python', '-mpip', 'install', '.')
return code: 1
expected return code: 0
stdout:
Executable `python` not found
stderr: (none)
Check the log at /home/username/.cache/pre-commit/pre-commit.log
使用虚拟环境时也会出现同样的错误(
virtualenv 20.16.5
):
$ virtualenv venv
created virtual environment CPython3.10.6.final.0-64 in 170ms
creator CPython3Posix(dest=/home/username/Projects/web/app/venv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/username/.local/share/virtualenv)
added seed packages: pip==22.2.2, setuptools==65.3.0, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
$ git commit -m "Update file."
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('python', '-mpip', 'install', '.')
return code: 1
expected return code: 0
stdout:
Executable `python` not found
stderr: (none)
Check the log at /home/username/.cache/pre-commit/pre-commit.log
这是完整的日志:
$ cat /home/username/.cache/pre-commit/pre-commit.log
pre-commit version: 2.20.0
git --version: git version 2.34.1
sys.version:
3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]
sys.executable: /usr/bin/python3
os.name: posix
sys.platform: linux
### error information is the same as here above
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/pre_commit/error_handler.py", line 73, in error_handler
yield
File "/usr/local/lib/python3.10/dist-packages/pre_commit/main.py", line 358, in main
return hook_impl(
File "/usr/local/lib/python3.10/dist-packages/pre_commit/commands/hook_impl.py", line 254, in hook_impl
return retv | run(config, store, ns)
File "/usr/local/lib/python3.10/dist-packages/pre_commit/commands/run.py", line 424, in run
install_hook_envs(to_install, store)
File "/usr/local/lib/python3.10/dist-packages/pre_commit/repository.py", line 223, in install_hook_envs
_hook_install(hook)
File "/usr/local/lib/python3.10/dist-packages/pre_commit/repository.py", line 79, in _hook_install
lang.install_environment(
File "/usr/local/lib/python3.10/dist-packages/pre_commit/languages/python.py", line 221, in install_environment
helpers.run_setup_cmd(prefix, install_cmd)
File "/usr/local/lib/python3.10/dist-packages/pre_commit/languages/helpers.py", line 48, in run_setup_cmd
cmd_output_b(*cmd, cwd=prefix.prefix_dir, **kwargs)
File "/usr/local/lib/python3.10/dist-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: ('python', '-mpip', 'install', '.')
return code: 1
expected return code: 0
stdout:
Executable `python` not found
stderr: (none)
这是我的
venv/
文件夹结构:
$ tree -L 3 venv/
venv/
├── lib
│ └── python3.10
├── local
│ ├── bin
│ │ ├── activate
│ │ ├── activate.csh
│ │ ├── activate.fish
│ │ ├── activate.nu
│ │ ├── activate.ps1
│ │ ├── activate_this.py
│ │ ├── deactivate.nu
│ │ ├── pip
│ │ ├── pip3
│ │ ├── pip-3.10
│ │ ├── pip3.10
│ │ ├── python -> /usr/bin/python3
│ │ ├── python3 -> python
│ │ ├── python3.10 -> python
│ │ ├── wheel
│ │ ├── wheel3
│ │ ├── wheel-3.10
│ │ └── wheel3.10
│ └── lib
│ └── python3.10
└── pyvenv.cfg
6 directories, 19 files
...这可能是造成麻烦的原因https://github.com/pypa/virtualenv/issues/2350但我绝对无法理解所有的故事。
更多:
$ which python # empty result!
$ which python3
/usr/bin/python3
$ ls -l /usr/bin/python3
(...) /usr/bin/python3 -> python3.10
我在预提交安装中遗漏了什么吗?
如果可能的话,我怎样才能正确且简单地(#basic user)修复此错误?
我遇到的问题存在于前段时间从 PyPi 安装的
virtualenv
版本 20.16.0
。
20.25.0
后,问题现已修复,并且 pre-commit
在 Ubuntu 22.04.3 上顺利运行:
$ pip3 install --upgrade virtualenv
请注意,来自
python3-virtualenv
官方存储库的 20.13.0+ds
(apt
) 软件包仍处于旧且有 bug 的状态:
$ apt-cache policy python3-virtualenv
python3-virtualenv:
Installed: 20.13.0+ds-2
Candidate: 20.13.0+ds-2
Version table:
*** 20.13.0+ds-2 500
500 http://ch.archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
500 http://ch.archive.ubuntu.com/ubuntu jammy/universe i386 Packages
100 /var/lib/dpkg/status
我不知道问题是什么时候在
virtualenv
中得到解决的,但感谢开发人员!