Sphere
Log in Join
☆ 10

Python 3.12 broke our import of distutils and setup.py dies

We upgraded the base image to python:3.12-slim and our build fails with ModuleNotFoundError: No module named 'distutils'. The setup.py imports from distutils.core import setup and has for years. I know distutils is gone in 3.12 but what's the actual minimal fix? We don't want to migrate the whole build to pyproject right now, just unblock the deploy. Deadline is tomorrow if that matters.

⚑ Report

4 replies

☆ 0
G ghost29 ·2026-10-03#1

Install setuptools — it provides a distutils shim through _distutils_hack as long as you import setuptools first. Add RUN pip install setuptools before the build step and change the import to from setuptools import setup, that's your one-line fix. The shim is enabled by default in recent setuptools versions.

⚑ Report
☆ 0
D duct ·2026-10-12#2

Changed the import, added setuptools to the Dockerfile. Now I get a different error: error: invalid command 'bdist_wheel'. So the wheel package isn't in there either. I swear the 3.11 image had all this baked in, but maybe that was a different base image entirely.

⚑ Report
☆ 0
F ferret21 ·2026-10-22#3

Right, add wheel too: pip install setuptools wheel. And if you build with python setup.py bdist_wheel, that's deprecated — pip wheel . is the supported path these days. You can absolutely keep setup.py, just stop invoking it directly. That buys you another year of not migrating.

⚑ Report
☆ 0
N null ·2026-10-31#4

While you're in there, pin the setuptools version in the build. We got burned when a new setuptools dropped the distutils shim entirely. Use setuptools<70 if you want the shim to keep working, or bite the bullet and add a minimal pyproject.toml with just a [build-system] table. Two lines, honestly worth it at this point.

⚑ Report
Log in to reply.