Should I install `pre-commit` into a python virtual environment or python global environment?

409 Views Asked by At

I'm developing python code on a Windows machine. When I have a new python package I am working on I create a virtual environment named .venv with python -m venv .venv. I activate the venv using .venv\Scripts\activate. When working on the package I install package dependencies such as numpy with (.venv) C:\...\my_package> pip install numpy.

I am trying to figure out how I should install the pre-commit package so that I can use pre commit hooks during version control of the package source code and related materials.

One workflow (following the philosophy of never installing anything in the base python install) would be to

  1. Activate the virtual environment
  2. install pre-commit into the virtual environment
  3. write the .pre-commmit-config.yaml file
  4. run pre-commit install in the package/repo directory
  5. Run git commands, ensuring the virtual environment is activated (otherwise pre-commit will not be able to run properly)

The major downside to this strategy is that it REQUIRES the virtual environment to be activated for git commands to work. This is a bit strange since normally you don't need python for git to work, and you might even be version controlling a repository that is not even python code!

The alternative workflow would be

  1. install pre-commit into the global python installation. Now pre-commit is available as a command line command anywhere on the system.
  2. write the .pre-commmit-config.yaml file
  3. run pre-commit install in the package/repo directory
  4. Run git commands, pre-commit should always work.

The downside here is you're messing with your global python installation and you may run into weirdness if e.g. you have multiple python installations on your system. A similar strategy might be to use a tool like pipx to do a "global" installation of pre-commit.

What is a recommended installation strategy for pre-commit?

1

There are 1 best solutions below

0
On

One nice solution to this problem is to use pipx. pipx essentially provides a middle ground between installing nothing to the global python installation and installing everything to the global python installation. pipx is installed into the global python installation and then you use pipx to (1) install (and manage) python-based command line tools (such as pre-commit) within isolated virtual environments and (2) expose those command line tools to the global terminal command namespace. The workflow in this case is:

  1. python -m pip install --user pipx to install pipx to the global python.
  2. pipx ensurepath to make sure path is setup correctly.
  3. pipx install pre-commit
  4. pre-commit install in the git repo of interest
  5. set up .pre-commit-config.yaml in the git repo of interest
  6. Now git commands should work using pre-commit (which uses the pre-commit installed by pipx) on the repo of interest, whether or not the virtual environment is activated.