Sphere
Log in Join

Fixing 'ModuleNotFoundError: No module named distutils' after a Python 3.12 upgrade

The first CI job to die after we moved to Python 3.12 was an old internal package that builds a C extension. It had been green on 3.11 for two years. The traceback:

Traceback (most recent call last):
File "/home/runner/work/legacy-core/setup.py", line 3, in <module>
from distutils.core import setup
ModuleNotFoundError: No module named 'distutils'

I stared at that for a minute, mostly because setup.py is the last place I expected to be debugging anything. Anyway, PEP 594 finally removed distutils from the standard library in 3.12, and this package still imported it directly.

## What actually changed

distutils was deprecated since 3.10 and slated for removal in 3.12. The recommended replacement is setuptools, which has shipped its own vendored copy for ages and exposes the same names. That sounds like a one-line import swap, and for a lot of packages it is. But there are two traps.

First, setuptools only injects its distutils shim when it's installed and importable in the environment that runs the build. If your build-system.requires still says ["setuptools>=42", "wheel"] you're usually fine, because pip creates an isolated build env and installs setuptools there. The failure above came from a job that ran python setup.py sdist directly, outside pip, in a bare venv where nothing had installed setuptools yet.

Second, some code does from distutils.sysconfig import get_python_inc and mixes it with sysconfig values. Those two disagree about paths on 3.12 more often than you'd think.

## The import swap that mostly works

For the simple case, change the import and move on:

python
# before
from distutils.core import setup, Extension

# after
from setuptools import setup, Extension

Do a grep across the repo for distutils — it hides in setup.py, in conftest.py, and occasionally in a random helper module someone added in 2019. We found four references, only one of which was in setup.py.

## Pinning setuptools where it matters

The real fix was making sure the build environment has a recent setuptools. We added a pyproject.toml because the package didn't have one:

toml
[build-system]
requires = ["setuptools>=68.0", "wheel"]
build-backend = "setuptools.build_meta"

68.0 is the first version I'd trust here; it handles the 3.12 distutils removal cleanly and dropped some older hacks. If you're stuck with an ancient setuptools you'll see a different error — AttributeError: module 'setuptools' has no attribute 'build_meta' — which is just telling you to upgrade.

Then stop calling setup.py directly in CI. Use python -m build:

bash
python -m pip install --upgrade build
python -m build --sdist --wheel

That forces the isolated build env declared in pyproject.toml, so the requires pin actually gets applied. Our old command sidestepped all of it.

## One more thing with editable installs

After that, pip install -e . started warning about a legacy setup.py develop fallback. That went away once the build backend was declared. If you're still seeing it, you're probably on pip 23.0 or older; pip 24.x handles PEP 660 editable installs properly.

I don't love that a dependency-free import needs a build-system section now, but it's the least annoying path that keeps the package buildable on both 3.11 and 3.12. If you know a cleaner way to keep an old C-extension build alive without dragging in setuptools at every layer, I'm all ears.