Pre-commit 2.0.0 does not work with conda based python in Windows.
Anaconda/miniconda based python installations require an activated conda environment before calling python or any other entry-point executable. This requirement especially affects conda running on Windows.
"pre-commit install" only stores the python executable in git hooks. This is not enough for conda based python installations if the environment of pre-commit is not activated when calling "git commit".
The following (rejected) pull request is intended to solve that problem: https://github.com/pre-commit/pre-commit/pull/1324
<problem reproduction instruction will be posted here, soon>
.pre-commit-config.yaml:
repos:
- repo: https://github.com/Quantco/pre-commit-mirrors-black
rev: 19.10b0
hooks:
- id: black-conda
args:
- --safe
- --target-version=py36
Good case with running git in activated conda environment 'pre-commit':
(pre-commit) C:\Temp\code\try_pre_commit>D:\Programs\Miniconda3\condabin\conda activate pre-commit
(pre-commit) C:\Temp\code\try_pre_commit>pre-commit install
pre-commit installed at .git\hooks\pre-commit
(pre-commit) C:\Temp\code\try_pre_commit>"C:\Program Files\Git\bin\git" commit
black-conda..........................................(no files to check)Skipped
On branch master
Untracked files:
t.sh
nothing added to commit but untracked files present
(pre-commit) C:\Temp\code\try_pre_commit>
Bad case when deactivating environment right afterwards:
(pre-commit) C:\Temp\code\try_pre_commit>D:\Programs\Miniconda3\condabin\conda deactivate
C:\Temp\code\try_pre_commit>"C:\Program Files\Git\bin\git" commit
Traceback (most recent call last):
File "D:\Programs\Miniconda3\envs\pre-commit\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\Programs\Miniconda3\envs\pre-commit\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "D:\Programs\Miniconda3\envs\pre-commit\lib\site-packages\pre_commit\__main__.py", line 1, in <module>
from pre_commit.main import main
File "D:\Programs\Miniconda3\envs\pre-commit\lib\site-packages\pre_commit\main.py", line 13, in <module>
from pre_commit.commands.autoupdate import autoupdate
File "D:\Programs\Miniconda3\envs\pre-commit\lib\site-packages\pre_commit\commands\autoupdate.py", line 17, in <module>
from pre_commit.clientlib import InvalidManifestError
File "D:\Programs\Miniconda3\envs\pre-commit\lib\site-packages\pre_commit\clientlib.py", line 16, in <module>
from pre_commit.error_handler import FatalError
File "D:\Programs\Miniconda3\envs\pre-commit\lib\site-packages\pre_commit\error_handler.py", line 10, in <module>
from pre_commit.store import Store
File "D:\Programs\Miniconda3\envs\pre-commit\lib\site-packages\pre_commit\store.py", line 4, in <module>
import sqlite3
File "D:\Programs\Miniconda3\envs\pre-commit\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "D:\Programs\Miniconda3\envs\pre-commit\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: The specified module could not be found.
this is a bug with conda, I'd suggest opening an issue there
The problem of running git within activated python environment where pre-commit is installed arises when using an IDE like PyCharm. There it is easy to configure the activation of different conda environments when running python, but it is not considered a useful feature for calling git
this is a bug with conda, I'd suggest opening an issue there
This is expected behavior for conda. You are not supposed to call a python installed by conda without activating that environment. I hate that behavior, but I like to use the powerful package manager capabilities which span beyond python dependencies..
pre-commit only extracts the path to the python executable when "pre-commit install" is called. This is not enough for a conda installed python.
then conda is violating some basic expectations of how python works and should be fixed
A workaround may be for you to suggest your patch to the conda packaging of pre-commit, but I do not want to support it here
I agree. I have ideas how conda should work instead. But I see no chance of getting that through. You would need to develop an alternative with the same complexity of features.
did this work for pre-commit 1.x?
I have a running system with pre-commit 1.12.0. The problem is that since more than one year, I see more and more actions with conda installed python tools to require activation.
that's not what I asked, does the problem reproduce on pre-commit 1.21.1
I assume the problem is not just the pre-commit version. It may also be a change in one of pre-commit's dependencies.
I doubt it, sqlite3 has been a python standard library dependency of pre-commit since 0.4.0 (2015-02-07)
It may be a version change of sqlite3. All I can say is, I have a pre-commit 1.xx working. And I have a pre-commit 1.xx installation not working. I would need to explore further.
And I had one colleague where downgrading pre-commit from 2.0.0 to 1.12.0 helped.
this can be managed if INSTALL_PYTHON can be managed.
heck, i just tried a wrapped python for python -mpre_commit install and it still picked up python.exe as the INSTALL_PYTHON!
seriously, why does INSTALL_PYTHON have priority over which(pre-commit) ???
if os.access(INSTALL_PYTHON, os.X_OK):
CMD = [INSTALL_PYTHON, '-mpre_commit']
elif which('pre-commit'):
CMD = ['pre-commit']
else:
raise SystemExit(DNE)
because it is slow to look up pre-commit on the path, and it is possible that there are other executables named pre-commit, and the python which was used to install pre-commit is the most likely to have access to pre-commit
please consider the tone of the language you're using, saying things like "seriously, why ... ???" is not appreciated
because it is slow to look up
pre-commiton the path, and it is possible that there are other executables namedpre-commit, and the python which was used to installpre-commitis the most likely to have access topre-commitplease consider the tone of the language you're using, saying things like "seriously, why ... ???" is not appreciated
apologies.