PythonMastery
reference 3 min read · lesson 75 of 80 in Errors

ModuleNotFoundError: No module named 'distutils'

1 · The lesson

read

What this error means

Some code ran import distutils, and your Python doesn't have it. distutils was an old part of the standard library for building and installing packages. It was marked for removal in Python 3.10 and taken out completely in Python 3.12 (PEP 632). Your own code is usually fine. The import is almost always inside an older package, or an old setup.py, that was written before 3.12 existed.

When you see it

Running a script that uses an older package:

python
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from distutils.version import LooseVersion
ModuleNotFoundError: No module named 'distutils'

During pip install of an older package, the same last line can appear near the end of a long build log, under [end of output].

NumPy had its own copy, numpy.distutils, which is also gone for Python 3.12 and newer:

python
ModuleNotFoundError: No module named 'numpy.distutils'

Why it happens

1. You moved to Python 3.12 or newer, and a package didn't. The package worked on 3.11, where distutils still existed. On 3.12 and later the import fails the moment the package loads.

2. pip is building an old version from source. You (or a requirements.txt file) asked for an exact old version of a package. There is no ready-made download of it for your Python version, so pip runs the package's old setup.py, and that file imports distutils.

3. A new virtual environment has no setuptools. Python 3.12 no longer puts setuptools into a new virtual environment by default. setuptools contains a copy of distutils, so an old project that worked in an older environment fails in a fresh one.

How to fix it

Upgrade the package that imports it. Read the traceback: the file path above the error line names the package. Almost every maintained package removed distutils long ago:

bash
python -m pip install --upgrade name-of-the-package

If a requirements.txt pins an exact old version (==), change the pin to a newer version, or loosen it to >=.

Install setuptools, which brings distutils back. setuptools includes its own copy of distutils and makes import distutils work again. This is the quickest fix for an old package you can't upgrade:

bash
python -m pip install setuptools

In your own code, use the replacement. The most common use was comparing version numbers with LooseVersion or StrictVersion. The packaging library does that job now. Install it first with pip install packaging:

python
from packaging.version import Version

print(Version("1.10") > Version("1.9"))     # True
print("1.10" > "1.9")                       # False: plain strings compare letter by letter

For other distutils functions: shutil.which() replaces distutils.spawn.find_executable(), shutil.copytree() replaces distutils.dir_util.copy_tree(), and sysconfig replaces distutils.sysconfig.

If nothing else works, use Python 3.11 for this project. Make a virtual environment with 3.11 (on Windows, py -3.11 -m venv .venv). Treat this as a short-term step while you look for a maintained version of the package.

When you'd see this in real code

  • An old tutorial's requirements.txt pins exact old versions, and pip install -r requirements.txt fails on Python 3.12 while building one of them from source.
  • A Docker image moves from python:3.11 to python:3.12, and an old dependency stops importing.
  • pip install of an old package from GitHub fails, because its setup.py begins with from distutils.core import setup.
  • A project that worked for years breaks after you upgrade Python with your system package manager.

See Also

Found this useful? Share it with a friend who’s learning Python.