Git Hooks inside PyCharm running in incorrect environment
Asked Answered
A

6

8

I have recently started a new project in PyCharm, finally utilizing anaconda environments. However, after trying to make my first commit through PyCharm, it appears to be using my native python, not the environment set in PyCharm. I've tried to restart PyCharm several times, restarted my computer, and reinstalled the virtual environment.

Here is a copy of pre-commit hook:

set -e

# Run linters and tests
source scripts/lint.sh

The linter is the following: (which python has been added to highlight the issue)

set -e
set -v

which python

flake8 ~project name~
mypy ~project name~
pytest -x
black --check --fast --quiet ~project name~

set +v

I am running the commit through PyCharm -> VCS -> Commit. Inside PyCharm, the commit fails

enter image description here (below this are a large amount of mypy errors, but note the environment)

However, if I run the commit from the terminal with $ git commit -m "testing commit" the commit works. It provides the following response:

enter image description here

This is the correct virtual environment inside of the project, seen here: enter image description here

Am I setting something up incorrectly? I vastly prefer PyCharm's VCS and would prefer not to have to use git from the terminal.

Adenocarcinoma answered 24/6, 2019 at 20:37 Comment(0)
P
7

PyCharm doesn't run git hooks under the virtual environment. The relevant ticket in the bug tracker: https://youtrack.jetbrains.com/issue/PY-12988

Performing answered 26/6, 2019 at 7:13 Comment(2)
Thank you for linking this ticket. Looking at it, I guess I can only learn to run git from the command line in the near future. Thank you.Adenocarcinoma
Issue was fixed in PyCharm 2021.2.Ghiselin
I
3

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.

Immiscible answered 4/12, 2020 at 17:43 Comment(1)
I know it's a long time since the question was asked, but this should be the accepted solution.Aftercare
O
1

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:

  1. Close PyCharm.
  2. cd /your/project/dir
  3. Open PyCharm from the command line: PYENV_VERSION="$(pyenv local | head -1)" open /Applications/PyCharm.app/. I'm using macOS, you should adapt the open 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.

Oilla answered 10/3, 2020 at 11:40 Comment(0)
F
0

You can manually edit the auto-generated pre-commit file (located in your project dir at .git/hooks/pre-commit) to add the path to your virtual environment, replacing:

# start templated
INSTALL_PYTHON = 'PATH/TO/YOUR/ENV/EXECUTABLE'

with

# start templated
INSTALL_PYTHON = 'PATH/TO/YOUR/ENV/EXECUTABLE'
os.environ['PATH'] = f'{os.path.dirname(INSTALL_PYTHON)}{os.pathsep}{os.environ["PATH"]}'
Furnishing answered 30/5, 2020 at 8:49 Comment(0)
S
0

None of the above solutions worked for me: PyCharm 2020.3 on Windows 10

What I did is to rename the .git\hooks\pre-commit -> .git\hooks\pre-commit.py

and created a new .git\hooks\pre-commit with next content:

#!/bin/bash

BASEDIR=$(dirname "$0")
/c/<PATH-to-YOUR-Python>/python.exe $BASEDIR/pre-commit.py $@

Worked as a charm!

Saporific answered 7/7, 2021 at 21:38 Comment(0)
P
0

PyCharm now does support running git hooks by pointing the python interpreter to whatever local venv you would like to use. See the update here: https://youtrack.jetbrains.com/issue/PY-12988/Pycharm-does-not-use-virtualenv-pep8-for-git-hooks#focus=Comments-27-4796459.0-0

Here are the steps I followed to confirm that PyCharm is using the correct venv and git hooks are enabled.

  • Go to Settings > Project > Python Interpreter >
  • Click the Add Interpreter link which opens a dropdown.
  • Select Add Local Interpreter
  • Select the Existing radio button
  • Click the three dot button to open the file dialog.
  • Select the folder for your venv and click OK

Finally, confirm that git hooks from your venv are enabled.

  • Go to Settings > Version Control > Git
  • Scroll all the way to the bottom of the Git settings (as of Pycharm v 2024.1)
  • Confirm that "Activate virtualenv for hooks" is checked.
  • Click OK to save and close Settings.

In the lower right corner of PyCharm you should see the Python Interpreter set to the version of Python used by your .venv followed by the name of the directory holding the .venv directory.

Now that the Python Interpreter is set to your local venv you will be able to use the PyCharm Git with your defined git hooks.

Phocine answered 10/6 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.