How do I fix modulenotfounderror: no module named 'distutils' when running a Python script?

I’m trying to run a Python script, but I keep getting this error:

modulenotfounderror: no module named 'distutils'

I thought distutils was included with Python by default, but it seems like it’s missing from my environment. I’m using Python 3.x on Linux (Ubuntu), and I installed Python via apt.

Has anyone dealt with this modulenotfounderror: no module named distutils issue before?

What’s the recommended way to install or restore the distutils module so I can continue working with setup tools and packages that depend on it?

This usually happens when Python is installed via apt on Ubuntu but the python3-distutils package isn’t included by default. It’s easy to miss, especially if you’re setting up a lightweight or minimal system.

You can fix it by installing the missing system package:

sudo apt-get update
sudo apt-get install python3-distutils

Make sure the version matches your installed Python (e.g., python3.10-distutils if you’re on Python 3.10). Once installed, the error should go away and any package that needs distutils like setuptools, should work as expected.