ModuleNotFoundError: No module named 'distutils'
1 · The lesson
readWhat 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:
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:
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:
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:
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:
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.txtpins exact old versions, andpip install -r requirements.txtfails on Python 3.12 while building one of them from source. - A Docker image moves from
python:3.11topython:3.12, and an old dependency stops importing. pip installof an old package from GitHub fails, because itssetup.pybegins withfrom distutils.core import setup.- A project that worked for years breaks after you upgrade Python with your system package manager.
Related errors
- ModuleNotFoundError: No module named 'X': the general case, a package that isn't installed or is installed into a different Python.
- error: Microsoft Visual C++ 14.0 or greater is required: the other common failure when pip builds an old package from source, on Windows.
- error: externally-managed-environment: pip refuses to install into the Python your operating system owns.
- ImportError: cannot import name 'X': the module is there, but the name you asked for isn't.
See Also
- All Python errors: the full index, by type and by when it happens.
- Virtual environments
- pip and the standard library
- Modules and imports (tutorial)
Found this useful? Share it with a friend who’s learning Python.