How to Resolve Python "ModuleNotFoundError: No module named 'distutils.util'"
Encountering ModuleNotFoundError: No module named 'distutils.util'
(or similar errors for distutils.cmd
or distutils.core
) in Python can be confusing, as distutils
is historically part of Python's standard library. However, on Debian-based Linux distributions like Ubuntu, distutils
is often packaged separately from the main Python interpreter.
This guide explains why this error typically occurs on these systems and provides the standard solutions using the apt
package manager.
Understanding the Error: distutils
and System Packaging
distutils
is a foundational package in Python used for building and installing Python modules. While included in the standard library by default in many Python installations (like those from python.org or Conda), Debian and Ubuntu often split parts of the standard library into separate system packages managed by apt
.
The ModuleNotFoundError
for distutils.util
, distutils.cmd
, or distutils.core
arises on these systems when Python tries to import these submodules, but the necessary python3-distutils
system package (or its version-specific equivalent) hasn't been installed alongside the main python3
interpreter package.
Common Cause: Missing python3-distutils
Package (Debian/Ubuntu)
The most frequent reason for this error on Debian-based systems is simply that the separate package containing the distutils
module was not installed or was removed.
Solution 1: Install python3-distutils
via apt
(Recommended)
The standard fix is to use the apt
package manager to install the missing package.
Check Your Python Version
First, determine the specific Python 3 version you are using, as you might need a version-specific package.
python3 --version
Output:
Python 3.10.6
Install the Correct Package
Open your terminal and run the following commands:
# 1. Update your package list
sudo apt update
# 2. Install the distutils package for your Python 3 version
# - For the system's default Python 3:
sudo apt install python3-distutils
# - For a specific version (e.g., 3.10 found above):
# Use this if you installed a specific python3.X package via apt
# or if the generic one doesn't work.
# sudo apt install python3.10-distutils
# sudo apt install python3.11-distutils # etc.
# 3. Optionally, ensure python3-apt is also installed (sometimes related)
sudo apt install python3-apt
- Always run
sudo apt update
first to ensure you have the latest package lists. - Start by trying the generic
python3-distutils
. If that doesn't resolve it or you know you're using a specificpython3.X
installed viaapt
(like from the Deadsnakes PPA, see below), install the version-specific package (e.g.,python3.10-distutils
). - After installation, try running your Python script or command again.
Solution 2: Reinstall python3-distutils
via apt
If you believe the package is installed but might be corrupted, you can try reinstalling it.
# Reinstall the generic package
sudo apt install --reinstall python3-distutils
# Or reinstall the version-specific package if needed
# sudo apt install --reinstall python3.10-distutils
Solution 3: Using Deadsnakes PPA for Specific Python Versions (Alternative)
If you installed a specific Python version (e.g., 3.11) on an older Ubuntu version using the popular Deadsnakes PPA, you'll likely need to install the corresponding distutils
package from that same PPA.
# 1. Add the Deadsnakes PPA (if you haven't already)
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
# 2. Install the distutils package matching your Python version from the PPA
# Replace X with your specific minor version (e.g., 11)
sudo apt install python3.X-distutils
# 3. Optionally install the corresponding apt package
sudo apt install python3.X-apt
Example for Python 3.11:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.11-distutils
sudo apt install python3.11-apt
Use this method if the standard Ubuntu repositories don't provide the distutils
package matching a specific Python version you installed from a PPA.
Important Context: Installation Method Matters
This ModuleNotFoundError
for distutils
is highly specific to Debian/Ubuntu systems managing Python via apt
. If you installed Python using other methods, distutils
is typically included:
- Official Python.org Installer (Windows/macOS): Includes
distutils
. - Homebrew (macOS): Includes
distutils
. - Conda/Anaconda: Installs Python with
distutils
included in its own environment. - pyenv: Installs self-contained Python versions which include
distutils
.
If you encounter this error on a non-Debian/Ubuntu system or with Python installed via these other methods, it might indicate a more severe problem with your Python installation itself, potentially requiring a repair or full reinstall of Python.
Conclusion
The ModuleNotFoundError: No module named 'distutils.util'
(or .cmd
, .core
) on Debian/Ubuntu systems almost always means the required python3-distutils
system package is missing.
The primary solution is to install it using the apt
package manager:
sudo apt update
sudo apt install python3-distutils
Remember to use the version-specific package name (e.g., python3.10-distutils
) if needed, especially when using PPAs like Deadsnakes. If issues persist, try reinstalling the package.