Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Hooks inside PyCharm running in incorrect environment

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.

like image 742
battr Avatar asked Jun 24 '19 20:06

battr


Video Answer


2 Answers

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

like image 135
Pavel Karateev Avatar answered Sep 21 '22 11:09

Pavel Karateev


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.

like image 20
andreoliwa Avatar answered Sep 22 '22 11:09

andreoliwa