2

I use pre-commit to run black flake8 and isort on my code.

I ran pre-commit install and as expected it created .git/hooks/pre-commit which starts like:

#!/usr/bin/env python3.9
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
import os
import sys
...

The hook works fine in the terminal:

$ git commit -am "remove commented block"
isort....................................................................Passed
black....................................................................Passed
flake8...................................................................Passed
[main f30007d] remove commented block
 1 file changed, 4 deletions(-)

But running it from VSCode's Source Control Panel yields an error (command output):

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
env: python3.9: No such file or directory

Not sure where that comes from. In addition (not sure it matters though) I double checked: both the terminal's python and VSCode's selected Python interpreter point to the same /Users/victor/.pyenv/shims/python

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
ted
  • 13,596
  • 9
  • 65
  • 107
  • Note: I'm aware of https://stackoverflow.com/questions/68634761/env-python3-9-no-such-file-or-directory but it's a different matter – ted Oct 31 '21 at 05:16
  • 1
    It's not Git or Python; it has something to do with the path that your shell is using (based on pathname I guess macOS and hence either bash or zsh most likely, so `$PATH`). Wherever you have `python3.9` installed, your *interactive* shell is able to find it, but your *non-interactive shell run from VSCode* isn't. How to fix that depends partly on the shell and partly on VSCode. – torek Oct 31 '21 at 06:57
  • I'm on a mac indeed, with zsh+oh-my-zsh. Any idea on what I should investigate next? – ted Oct 31 '21 at 07:02
  • 1
    I don't use zsh but it tends to follow the standard setup of other shells: .zshrc, .zsh_login, etc., I think. Programs generally inherit $PATH from earlier programs, so if your $PATH is set correctly in your login shell, it remain set correctly in all subsequent programs. This means that bash users should put PATH-setting in `.profile` or `.bash_profile`, csh users should put it in `.login`, and so on; find the zsh login file, whatever they call it... – torek Oct 31 '21 at 08:34
  • run `which -a python3.9` outside of vs code, and then inside of vs code figure out why that directory isn't on your `PATH` (it's likely due to missing some bashrc / etc. file) – anthony sottile Oct 31 '21 at 15:07

1 Answers1

0

It seemed to be an issue with pyenv not being loaded by VSCode's source control panel when executing the git commands.

I tried moving some stuff (like $(pyenv init -)) into an earlier zsh configuration file like .zshenv but that did not help.

In the end, specifying the complete path fixed it

#!/usr/bin/env /Users/victor/.pyenv/shims/python3.9
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
import os
import sys
...
ted
  • 13,596
  • 9
  • 65
  • 107