hakk

software development, devops, and other drivel
Tree lined path

ModuleNotFoundError: No module named 'distutils.util' on Ubuntu

Not on Ubuntu?

If you’re using Ubuntu 16.04 or newer and want to use Python 3.7 and newer with pip you have probably seen this error message ModuleNotFoundError: No module named 'distutils.util'.

On later versions of Ubuntu the error message will be: ModuleNotFoundError: No module named 'distutils.cmd'. This fix will still work.

bmcculley@hakk:~$ pip3

Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/cmdoptions.py", line 19, in <module>
    from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils.util'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/usr/local/lib/python3.7/dist-packages/pip/_internal/cli/cmdoptions.py", line 19, in <module>
    from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils.util'

Solution

Don’t worry, it’s a pretty easy fix. The module not found error just means that the nessacary packages aren’t installed. To fix this, just run the following in the terminal:

sudo apt install python3-distutils

sudo apt install python3-apt

But they’re already installed…

If you upgraded the system with them already installed the packages might have been corrupted during the upgrade. It might be possible to fix this by reinstalling the packages (run as needed):

sudo apt install --reinstall python3-distutils

sudo apt install --reinstall python3-apt

Hopefully this has helped and you’re back up and running.

Not on Ubuntu and using Python 3.12 or Newer

In the release highlights for Python 3.12, they note that distutils package has been removed.

Of note, the distutils package has been removed from the standard library.

Further down it’s noted that “The third-party Setuptools package continues to provide distutils, if you still require it in Python 3.12 and beyond.”

Finally, they recommend only installing in an activated virtual environment.

gh-95299: Do not pre-install setuptools in virtual environments created with venv. This means that distutils, setuptools, pkg_resources, and easy_install will no longer available by default; to access these run pip install setuptools in the activated virtual environment.

TL;DR: Create an virtual environment, activate it and then install setuptools.


pip install setuptools

Reference: https://docs.python.org/3/whatsnew/3.12.html#summary-release-highlights