How to use pipx with pyenv

How to use pipx with pyenv
Photo by Nielsen Ramon / Unsplash

πŸ‘¨πŸ»β€πŸ’» TL;DR

In my case with fish I added this line to my config.fish:

set -gx PIPX_DEFAULT_PYTHON "$HOME/.pyenv/versions/3.8.5/bin/python"

It's roughly the bash equivalent for

export PIPX_DEFAULT_PYTHON="$HOME/.pyenv/versions/3.8.5/bin/python"

πŸ“– Backstory

As a Python developer, my workflow often involves running multiple versions of Python on different projects. To help me in this task, I use pyenv which is a wonderful tool to easily install and manage multiple Python versions on your system.

I also have a Python version installed via Homebrew for some formulae that require it. This version is the default for any script if pyenv doesn't specify any version.

Finally, I also use pipx which allows me to install python packages in their own virtual environment without messing with my system installation and still have them ready for use on the command line.

My problem is that pipx will by default use the Python version provided by Homebrew to install the executables. As I keep my system up to date with Homebrew, the Python version often updates. When that happens, all of my pipx packages break and I have to reinstall them all. Granted, pipx provides a command to do that easily but I'd still like to avoid the operation.

Thankfully, as documented[1], pipx supports an environment variable called PIPX_DEFAULT_PYTHON. You just need to point it to your preferred Python interpreter and be done with it!

πŸ’‘
I've now switched from pyenv to rtx but the concept remains the same: install python with rtx, then set PIPX_DEFAULT_PYTHON.

  1. Also available when running pipx --help β†©οΈŽ