virtualenvpython-3.10pre-commitpre-commit.comubuntu-22.04

pre-commit: Executable python not found when committing a new file


I'm trying to use pre-commit 2.20.0 on Ubuntu 22.04.1 / Python 3.10.6.

I followed the instructions at https://pre-commit.com/#installation and installed pre-commit with pip (22.2.2).

Here's my .pre-commit-config.yaml file:

$ 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

But I face an error when committing a new file:

$ 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

The same error occurs when using a virtual environment (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

Here's the full 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)

Here is my venv/ folder structure:

$ 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

...which may be the cause of the trouble as per https://github.com/pypa/virtualenv/issues/2350 but I can definitely not understand all the story.

More:

$ which python # empty result!

$ which python3
/usr/bin/python3

$ ls -l /usr/bin/python3
(...) /usr/bin/python3 -> python3.10

Did I miss something in the pre-commit installation?
How could I properly and simply (#basic user) fix this error, if possible?


Solution

  • The issue I faced existed with virtualenv version 20.16.0 installed from PyPi some time ago.

    After an update to 20.25.0, the issue is now fixed and pre-commit runs smoothly on Ubuntu 22.04.3:

    $ pip3 install --upgrade virtualenv
    

    Beware, the python3-virtualenv (20.13.0+ds) package from apt official repositories is still in an old yet buggy state:

    $ 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
    

    I don't know exactly since when the problem was fixed in virtualenv, but thanks to the devs!