Search code examples
pythongithomebrewpre-commitpre-commit.com

How can I use brew and install precommit for Python 3.6.8


I want to use brew to install pre-commit for python 3.6.8. I am using macOS Catalina (Version 10.15.7)

I am using brew install pre-commit to do the installation.

I am having issues because my pre-commit uses Python 3.9 and I want to avoid this (i.e. make brew pre-commit use python 3.6).

When I try brew install pre-commit, I find that automatically one sees the message Downloading https://homebrew.bintray.com/bottles/python%403.9-3.9.0_2.catalina.bottle.tar.gz.

The environment that I am on is python 3.6.8. ( python -V returns python 3.6.8).

Also,

username@USERNAME-M-F1AU % echo "$PATH" 
/Users/username/.pyenv/shims:/Users/username/.pyenv/shims:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

I also have, python 3.9 ([email protected]), (although it is not the active environment) in /usr/local/Cellar. I also have a folder for pre-commit inside /usr/local/Cellar. Some paths inside subfolders which are inside /usr/local/Cellar/pre-commit are linked to python 3.9. Following are examples of the same.

python3 -> ../../../../../opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9
python -> ../../../../../opt/[email protected]/Frameworks/Python.framework/Versions/3.9/bin/python3.9

The first line of /usr/local/bin/pre-commit is:

#!/usr/local/Cellar/pre-commit/2.9.0/libexec/bin/python

I have tried the following already:

  • Change of Python environment to Python 3.6. Issue was not solved here (symlinks of pre-commit with python 3.9)
  • Deletion of cache (rm -r /Users/username/.cache/pre-commit). Same thing happened here (symlinks of pre-commit with python 3.9 still existed).
  • Installing of precommit again (rm -r /usr/local/Cellar/pre-commit followed by brew install pre-commit). Same thing happened here (symlinks of pre-commit with python 3.9).
  • Removing and readjusting symlinks using the method shown below.
ln -nfs /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /usr/local/Cellar/pre-commit/2.9.0/libexec/bin/python
ln -nfs /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /usr/local/Cellar/pre-commit/2.9.0/libexec/bin/python3

Here, I got the following error /usr/local/Cellar/pre-commit/2.9.0/libexec/bin/python3: No module named pre_commit

What is the best way in which I can use brew to install pre-commit such that it uses python 3.6 and not python 3.9?

I saw this brew precommit page. It looks like brew install pre-commit automatically always uses python 3.9.0.

Any help will be appreciated.


Solution

  • brew only really works with the latest versions of software -- they package bottles against the latest version of python

    you can pin against older formulas, though at this point the 3.6 packages no longer cleanly build (I tried, as a way to show you how to do this, but they've bitrotted)

    since it looks like you're using pyenv -- probably your best bet is to install a python3.6 using that and then use pip to install pre-commit from there

    I don't use pyenv myself, but if I recall correctly you'd run:

    pyenv install 3.6.12
    pyenv global 3.6.12
    

    and then you would use python3.6 -m pip install pre-commit --user (you may need to add the --user bin directory to your PATH using your .bashrc / .zshrc)


    disclaimer: I'm the creator of pre-commit and I've contributed a bit to the brew formula for it