Failed to commit "/usr/bin/env: 'python.exe': No such file or directory when using Before Commit git hooks option
I am using pycharm on a windows machine here is the blow settings of windows/pycharm
PyCharm 2019.2.4 (Community Edition)
Build #PC-192.7142.42, built on October 31, 2019
Runtime version: 11.0.4+10-b304.77 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1994M
Cores: 4
Registry:
Non-Bundled Plugins: com.leinardi.pycharm.pylint, mobi.hsz.idea.gitignore, org.intellij.plugins.markdown
I recently installed pre-commit into my python3.7 workflow (my python I'm running from Anaconda).
My .pre-commit-config.yaml file contains:
# see https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3.7
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.0.0
hooks:
- id: flake8
I can run my pre-commit git hooks through the command line no problem, however, when I try to commit from Pycharm I get an error saying
"0 files committed, 1 file failed to commit: "my commit message" /usr/bin/env: 'python.exe': No such file or directory.
Is this expected behavior? When I check pycharm project structure/interpreter it shows it knows where my python.exe is so can this not be reflected in git bash if I am trying to commit through pycharm? I saw online I could add python.exe to my PATH environment variable on windows, however I have to change between different versions of python between projects and I don't want anything to get mixed up.
请先登录再写评论。
Could you provide more information about your pre-commit configuration?
Hi Andrey Resler - I have updated the original post with information about the pre-commit configuration. Please let me know if there is other info I should be including? Thanks
> I saw online I could add python.exe to my PATH environment variable on windows
That's what you probably need to do, or edit the pre-commit hook itself to include an exact path to python.
Git Integrations and Python integration are in fact not related. The reason for the error is environment inheritance. Git called by PyCharm inherits its environment, and PyCharm, in turn, inherits its settings from the parent process, which his usually the system shell. As ~Python is not available in the Windows PATH, it is not available in PyCharm context, and consecutively, in git context - thus the hook fails.
Hi Alex, Dmitriy, thanks for investigating this. I have the same problem on linux as well, e.g:
This workaround works:
```
PATH=$PATH:/home/mwouts/conda/envs/notebook_hooks/bin/ pycharm-professional
```
But I would prefer if PyCharm (who knows about my conda environment) could find that automatically :)
I had a similar problem after upgrading the system python (new 3.9 old 3.8) and reinstalling the git hooks helped at least in that circumstance:
It seems that pre-commit hooks do not use the virtual env but system python on purpose: https://github.com/pre-commit/pre-commit/issues/1522