Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run pre-commit in anaconda environment

I have an Anaconda environment in my current project. I have been trying to configure my pre-commit hooks, however I keep running unto this error:

enter image description here

So, the first time round when I got this error, I did some research and saw that ruamel.yaml needs to be installed. So, I did a pip install of ruamel.yaml and I did conform that the latest version is installed:

enter image description here

However, the error still persists. The most relevant posts I saw on the topic don't really seem to solve the problem:

https://github.com/pre-commit/pre-commit-hooks/issues/353

How can I get rid of this error?

like image 302
UGuntupalli Avatar asked Sep 21 '25 11:09

UGuntupalli


1 Answers

Short answer

I have found this work-around for the problem:

Install and configure pre-commit in your base conda environment instead of your project's conda environment.

Details

I just reproduced this error a year later, getting an identical error log.

This happened when I was trying to run git commit with a Conda environment activated on Windows. While in that conda environment I was able to install packages, the mechanism by which pre-commit installs its hooks ended up broken, with SSL unavailable and the same log shown in the question.

The fix for me was to deactivate my conda environment, install pre-commit in my base conda environment, and do one commit with base activated. For some reason I still don't understand, SSL is available and works fine when pre-commit installs its hooks from the base conda env, but not from an activated project-specific environment.

Once the pre-commit hooks are installed and configured, then I can activate my project's conda environment and do further commits from there, since pre-commit reuses its cached installation of the hooks.

Full error log

Here's the error log on my machine, identical to what we can see on the screen shot in the question:

(my-conda-env) $ git commit
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('C:\\Users\\someone\\.cache\\pre-commit\\repot4mg0fom\\py_env-default\\Scripts\\python.EXE', '-mpip', 'install', '.')
return code: 1
expected return code: 0
stdout:
    Processing c:\users\someone\.cache\pre-commit\repot4mg0fom
      Preparing metadata (setup.py): started
      Preparing metadata (setup.py): finished with status 'done'
    Could not fetch URL https://pypi.org/simple/ruamel-yaml/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/ruamel-yaml/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

stderr:
    WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ruamel-yaml/
    WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ruamel-yaml/
    WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ruamel-yaml/
    WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ruamel-yaml/
    WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ruamel-yaml/
    ERROR: Could not find a version that satisfies the requirement ruamel.yaml>=0.15 (from pre-commit-hooks) (from versions: none)
    ERROR: No matching distribution found for ruamel.yaml>=0.15

Check the log at C:\Users\someone\.cache\pre-commit\pre-commit.log

After running conda deactivate:

(base) $ git commit
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
check yaml...........................................(no files to check)Skipped
check json...........................................(no files to check)Skipped
fix end of files.....................................(no files to check)Skipped
[... rest of my checks]

I totally agree that this solution has bad smells: it's a work-around not a real fix. I agree that one should not install stuff in the base environment, and that all the project dependencies, including dev ones, should live in the project's venv. But... on Windows, this bug has affected both OP and myself.

2024 addendum

Facing this problem yet again, I have noticed that .git/hooks/pre-commit saves the path to the Python executable that was used to run pre-commit install. This time, I have found all previous solutions not working until I re-created that file by running pre-commit install from a clean environment created using a Python installation downloaded from python.org. Next time I face this issue, I'll experiment with re-running pre-commit install first to see if it solves the actual underlying problem.

Note that the environment that was active when running pre-commit install should be kept around. When I deleted it, things broke and I had to start again.

PS: this unsatisfyingly means that the answer to "how can I get pre-commit working with conda on Windows?" might be "don't". But installing pre-commit from a python.org Python installation and then activating conda works.

like image 164
someone Avatar answered Sep 23 '25 01:09

someone