pythonsetuptoolsdistutils

What does the 'distutils_hack' module do?


I was looking through my pip list and cleaning all my third party modules when I came across a module named 'distutils_hack'. I don't remember installing this, is this something I should be concerned about? The version I was using was Python 3.9.


Solution

  • It's used by setuptools to replace the stdlib distutils with setuptools' bundled distutils library.

    Quoting ncoghlan from pypa/setuptools#417 on why this is necessary:

    While CPython as a whole has many contributors, we don't have many folks contributing to distutils any more - folks need build tools that let them target currently deployed versions of Python, which setuptools provides, but distutils really doesn't.

    So the challenge for pypa/packaging-problems#127 is as follows:

    • assume that in some future version of Python (3.9? 3.10? 4.0? We dunno yet) distutils is no longer in the standard library per se, but is instead bundled the way we bundle pip (i.e. by installing it from a wheel at Python installation time).
    • eventually, for new Python versions (where distutils isn't in the standard library), we'd like the "real" distutils code to live in setuptools, and the code you get when you do import distutils to just be a backward compatibility shim that aliases the setuptools components into the right place
    • however, for old Python versions (where distutils is still in the standard library), then setuptools will still need to do its monkeypatching magic to allow plain distutils projects to emit the installation database metadata and to be built as wheel archives

    The scope of the eventual distutils-compat shim probably wouldn't need to be the full distutils API, but it wouldn't be the empty set either.

    So @jaraco's suggestion sounds like a reasonable starting point to me: give setuptools its own pre-patched copy of distutils so it can work on a distutils-free copy of Python, without providing an importable distutils facade.

    Building a suitable facade (with a setuptools dependency) would then be part of removing distutils from the standard library.

    Here is where you can find the module's source code: https://github.com/pypa/setuptools/blob/main/_distutils_hack/__init__.py