Perils of Pip Pinning in Production

just pip freeze it, bro

Tell me if this sounds familiar. You have your python project, and you want to make it ready for production/deployment/tech transfer. It’s been sitting on your system for a few months and now you have to get your coworker/devops/that-one-dude-obsessed-with-docker to build your project, reliably from scratch. You say, I know, I’ll just pip freeze > requirements.txt and then the install script can just pip install -r requirements.txt and you’re golden! Right?

What could possibly go wrong?

Well, you’ll likely be able to restore that state exactly. Exact same python, exact same packages.

overconstrained

The first problem is the way pip freeze expresses its state. Your pip freeze looks something like this:

absl-py==0.9.0
attrs==19.3.0
...
urllib3==1.25.7
Werkzeug==0.16.0
wrapt==1.10.11

It lists every single dependency, however important or unimportant, pinned to the most minute version. A snapshot frozen in time. However, the world around us is not static. Libraries update, sometimes for critical security reasons. You may want to add more dependencies. Eventually, the time comes where you want or need to modify the dependencies. You go to pip install it and…everything breaks.

A better way

Let your dependencies breathe. This is more art than science, but try to give a library version degrees of freedom proportional to how much you “trust” that library. By that, I mean how committed is the development team to using semver properly, how (un)likely are they to push a breaking change without bumping the appropriate version part. Numpy is old, stable, out of zerover, used by thousands, and gives long lead times on warnings about deprecation. I use numpy >= 1.21, <2.0 (numpy = ^1.21 in poetry notation). Scikit-learn, on the other hand, I usually pin to the minor version, because they have broken internal interfaces on me in the past. Normally, consumers should not care about private interfaces, however the scikit-prescribed way to save models is to use pickle. This means in practice, changing scikit’s version breaks your data science team’s workflow.

The future

As of the past four months, I’ve started leaning heavily into poetry. It’s not without its bugs, but it’s by and large stable. The CLI UX is fresh and intuitive, and it provides all kinds of useful subcommands to facilitate versioning, building, and publishing. The pyproject.toml format is definitely the future of python install configuration (goodbye, setup.py!) and provides a one-stop shop for configuring development tooling (except for flake8 ). While the solver introduces its own learning curve, it’s well and beyond a better experience overall for avoiding dependency hell compared to requirements.txt.

Written on January 30, 2020