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:
python -m pip install --user pipxto installpipxto the global python.pipx ensurepathto make sure path is setup correctly.pipx install pre-commitpre-commit installin the git repo of interest- set up
.pre-commit-config.yamlin the git repo of interest - Now
gitcommands should work usingpre-commit(which uses thepre-commitinstalled bypipx) on the repo of interest, whether or not the virtual environment is activated.
» pip install pre-commit
Videos
I know that Python dependency and environment management is a messy topic, so not trying to start any flame wars or anything, just looking for practical advice.
My usual tooling is
-
asdfto install different versions of Python -
poetryfor project management -
pipxfor CLI tools written in Python that are useful in general and aren’t a dependency on a project
This article seems to have very solid advice overall, but I am unsure if installing pre-commit per project is the best way.
In other words, is poetry or pipx ideal for pre-commit?
I realize that everyone will have different needs and preferences, so curious what the community thinks.
Edit: to the downvoters, can you kindly drop a comment why you think this is a stupid question?
» pip install pre-commit-hooks
» pip install pre-commit-uv