I saw this post by Trey Hunner about pip isolation, and I wanted to share a third method.

I’ve just updated my ~/.config/pip/pip.conf & my dotfiles repo to disallow pip installing outside virtual environments! 🎉

TIL 2 things about #Python’s pip:

  1. pip has a config file. If I ever knew this, I’d forgotten.

  2. pip has an option that stops it from working outside of a virtual environment!

https://mastodon.social/@treyhunner/112032637878747686

To Trey’s point, I never pip to install to easily install anything globally. If I want something installed globally, I can jump through a few hoops to avoid polluting my global pip cache.

My preferred way of disallowing pip installation outside virtual environments is to use the PIP_REQUIRE_VIRTUALENV environment variable.

I have export PIP_REQUIRE_VIRTUALENV=true set in my .bash_profile, which is part of my dotfiles. I prefer the ENV approach because I share my files over many computers, and it’s one less file to keep up with.

When I want to pip install something globally, I use pipx, which installs each Python application into its isolated environment.

For the few times that I do need to install a Python application globally, I use:

PIP_REQUIRE_VIRTUALENV=false python -m pip install \
    --upgrade \
    pip \
    pipx

I have this recipe baked into my global justfile so I can quickly apply upgrades.