Git Hooks inside PyCharm running in incorrect environment
PyCharm doesn't run git hooks under the virtual environment. The relevant ticket in the bug tracker: https://youtrack.jetbrains.com/issue/PY-12988
It seems that the aforementioned PyCharm ticket won't be fixed soon (it's there since 2014).
This hack below works for me; I added this to the PyCharm ticket:
This is a slightly annoying workaround that works for me:
- Close PyCharm.
cd /your/project/dir
- Open PyCharm from the command line:
PYENV_VERSION="$(pyenv local | head -1)" open /Applications/PyCharm.app/
. I'm using macOS, you should adapt theopen
command to your OS.I have to do it every time I switch projects, otherwise the
pylint
pre-commit hook doesn't work. If you have a similar config for your projects (Python version and not using PyLint), just run PyCharm from the CLI once.
Here's what worked for me.
I was getting the following error:
18:37 Commit failed with error 0 file committed, 1 file failed to commit: Update pre-commit hooks env: python3.7: No such file or directory
When I navigated to .git/hooks/pre-commit
in my project repo, it turned out that the shebang line is #!/usr/bin/env python3.7
.
This was an issue, since calling python3.7 on my MacOS would end up with the following:
zsh: command not found: python3.7
I could have either added a global python3.7 or, alternatively, updated the shebang. I went with the latter one and changed the shebang line to:
#!/usr/bin/env python3
This resolved the issue for me.