[BUG] Restore `pkg_resources`

· GitHub ·

5 min read Original article ↗

It is worth trying the pip environment variables described in 4966178/docs/deprecated/sdist-reproducibility.rst. I have not tested every possible tool combination, but in principle this should work. In general environment variables are inherited in subprocesses, but some tools may require you to explicitly allowlist these environment variables if they employ env var sanitisation.

Oh, I'm aware of PIP_CONSTRAINT. However, there are issues with using this environment variable for testing, when the package under test is part of a global constraints file like OpenStack uses. This was the rationale for adding native constraints support to tox. The behavior of PIP_CONSTRAINT has also changed significantly as of pip 25.3 with the introduction of PIP_BUILD_CONSTRAINT, which is something people need to bear in mind, particularly for stable branches.

so I'm not sure why we can remove pkg_resources so easily.

Just to add a bit of context here, this has been many years in the making:

  • A large amount of work went into untangling long‑standing architectural issues and introducing suitable replacements, including importlib.metadata and importlib.resources (see discussion in Setuptools should not rely on pkg_resources #3085).

I'm aware of the work that has gone in here, and I'm not dismissing any of the arguments in favour of its deprecation and replacement. However, deprecation != deprecation for removal. optparse was deprecated for 13 years (3.2 - 3.12). It wasn't deprecated for removal, and I don't believe anyone seriously considered removing it until perhaps PEP-594. PEP-594 in fact then led to optparse's explicitly un-deprecation because "[a]lthough it has been deprecated for many years, it’s still too widely used to remove it" (per here. I don't see any reason to undeprecate pkg_resources, since it doesn't do anything importlib.metadata can't, but I find it difficult to see how pkg_resources does not fall into the same camp with regards to its widespread use preventing removal.

  • Deprecation was introduced around five years ago (8fe85c2), with official docs urging users to migrate. Since then, nearly every discussion involving pkg_resources in setuptools/PyPA channels reiterated its deprecated status and pointed to alternatives.

Once again, deprecation != deprecation for removal. It was only deprecated for removal less than 9 months ago.

Regarding the replacement, importlib.metadata changed its API quite significantly in the early days, and was provisional in 3.8 and 3.9. Even after being made non-provisional, there was significant change in 3.12 (iirc), which broke libraries I help maintain like stevedore. I certainly didn't consider it stable until quite recently, and I suspect we are all aware of similar criticism that came from some large figures in the Python community. While it may be stable now, the pkg_resources deprecation was almost certainly premature given that its replacement wasn't stabilised until either Python 3.10 (October 2021) or Python 3.12 (October 2023), depending on your perspective.

In addition, the removal of pkg_resources indicates that we expect all consumers to have migrated across by now. Perhaps this is a fair assumption for main / master branches (though I suspect a lot of users of unmaintained by functional packages might disagree), but it wholly ignores maintained stable branches. Virtually all of OpenStack's stable branches are now broken since we only merged the final patches to multiple projects to remove pkg_resources dependencies in the last release or two (@jaraco is aware of this to a degree, since he very helpfully communicated the need for these changes with pbr.

  • Even before formal deprecation, there was broad consensus that the design of pkg_resources had fundamental issues, with significant unintended side effects and known performance problems. Fixing these would have required dropping most of the API, and at that point it was basically as easy as deprecating the whole package.

Again, I'm not arguing against the deprecation of this package. I'm arguing against its removal, particularly in this manner (not tied to a Python release, for example).

I double checked this to ensure I wasn't missing something, but by default you don't see deprecation warnings when installing packages that rely on pkg_resources at build-time. You only see these warnings at runtime. This is a large factor in why we (OpenStack) didn't rush its replacement in pbr more: we didn't see it.

pip-install.log

Even if you did see these warnings, as an end-user of a package, you also have limited the ability to address their use of a deprecated pkg_resources library.

EDIT: On the point of sdist reproducibility: I don't have the ability to do this for older releases of my projects, let along those of all my dependencies. This is a great goal to aim for, but it doesn't help the current situation.

I understand that this require some effort, but it is good practice (and you possible can do it once and reuse elsewhere). Not only because of setuptools, but for any other dependency that you have. Tooling drift is a reality that everyone is subject to, so it is very good to have at least a side CI-job (outside the critical path, e.g. that run every 1 month) that will at least make some noise for warnings, so that you are prepared when the final changes come.

I appreciate what you're trying to say here, but I'll just reiterate what I said previously.

setuptools has long held a blessed position in Python and there is a massive corpus of released packages that depend on its APIs. pkg_resources is one of those APIs and its removal is functionally equivalent to removing the setup function for these released packages. We obviously wouldn't remove the setup function, at least not without serious considered (likely a PEP), so I'm not sure why we can remove pkg_resources so easily.

Reacted by Jiachun Tao, Gabe Lyons, Daniel Rasmussen, Tully Foote, tsuna, Kevin Campbell, Eli Sallé and Lance Barto

Reacted by vivodi

Reacted by vivodi