pre-commit icon indicating copy to clipboard operation
pre-commit copied to clipboard

Pre-commit 2.0.0 does not work with conda based python in Windows.

Open QuantCo-mr-T opened this issue 6 years ago • 37 comments

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>

QuantCo-mr-T avatar Feb 11 '20 13:02 QuantCo-mr-T

.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.

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

this is a bug with conda, I'd suggest opening an issue there

asottile avatar Feb 11 '20 15:02 asottile

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

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

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..

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

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.

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

then conda is violating some basic expectations of how python works and should be fixed

asottile avatar Feb 11 '20 15:02 asottile

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

asottile avatar Feb 11 '20 15:02 asottile

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.

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

did this work for pre-commit 1.x?

asottile avatar Feb 11 '20 15:02 asottile

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.

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

that's not what I asked, does the problem reproduce on pre-commit 1.21.1

asottile avatar Feb 11 '20 15:02 asottile

I assume the problem is not just the pre-commit version. It may also be a change in one of pre-commit's dependencies.

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

I doubt it, sqlite3 has been a python standard library dependency of pre-commit since 0.4.0 (2015-02-07)

asottile avatar Feb 11 '20 15:02 asottile

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.

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

And I had one colleague where downgrading pre-commit from 2.0.0 to 1.12.0 helped.

QuantCo-mr-T avatar Feb 11 '20 15:02 QuantCo-mr-T

this can be managed if INSTALL_PYTHON can be managed.

majidaldo avatar Jul 27 '20 20:07 majidaldo

heck, i just tried a wrapped python for python -mpre_commit install and it still picked up python.exe as the INSTALL_PYTHON!

majidaldo avatar Jul 27 '20 21:07 majidaldo

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)

majidaldo avatar Jul 27 '20 21:07 majidaldo

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

asottile avatar Jul 27 '20 21:07 asottile

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

apologies.

majidaldo avatar Jul 27 '20 22:07 majidaldo