ModuleNotFoundError: No module named 'distutils.util'

After updating to Ubuntu 20.04, whenever I run:

pip install <some package>

or

pip --version

it says:

adam@daryy:~$ 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'

even when I normally type pip3 or pip.

1

10 Answers

The module not found likely means the packages aren't installed.

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

If they're already installed you can try to fix anything that may have been messed up in the upgrade with...

sudo apt-get install --reinstall package-name
10

I got this problem after upgrading to Ubuntu 20.04. I had a virtual environment depending on Python 3.7, and, to avoid re-installing the whole virtual environment for Python 3.8, I fixed distutils on Python 3.7:

I added the deadsnake PPA:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

And then installed the distutils package for Python 3.7:

sudo apt install python3.7-distutils

Note: for some reason I had an error installing the latter, that I solved this way:

sudo dpkg -i --force-overwrite /var/cache/apt/archives/python3.7-distutils_3.7.9-1+focal1_all.deb
sudo apt-get -f install 
3

Debian has decided that distutils is not a core python package, so it is not included in the last versions of debian and debian-based OSes. You should be able to do sudo apt install python3-distutils and it should work.

However, it did not work for me. I use Parrot OS, which is, as Ubuntu, Debian based. I upgraded my system and pip stopped working for python3.7, and I also got the error ModuleNotFoundError: No module named 'distutils.util'

I tried a lot of stuff to fix it and to reinstall distutils, and I found out by pure luck, that pip3, for python3.8 did work. I then tried python3.7 -m pip3 -V, got /usr/bin/python3.7: No module named pip3 so I decided to have a look in the /usr/lib files.

I looked at /usr/lib/python3/dist-packages and everything looked fine. Then I looked at /usr/lib/python3.7 and saw the folder distutil.

I opened it, and saw the __pycache__, the __init__.py file and a version.py file. I had no idea how many files should be in there, or what the code should be, but I knew that those two files were either wrong or missing another file.

Then I had a look at what was inside /usr/lib/python3.8/distutil and it was totally different. I found the following files:

command Folder
__pycache__ Folder
archive_util.py Python script
bcppcompiler.py Python script
cmd.py Python script
config.py Python script
core.py Python script
cygwinccompiler.py Python script
debug.py Python script
dep_util.py Python script
errors.py Python script
extension.py Python script
fancy_getopt.py Python script
filelist.py Python script
file_util.py Python script
__init__.py Python script
log.py Python script
msvc9compiler.py Python script
_msvccompiler.py Python script
msvccompiler.py Python script
README Plain text file
spawn.py Python script
sysconfig.py Python script
text_file.py Python script
unixccompiler.py Python script
util.py Python script
version.py Python script
versionpredicate.py Python script

This was a lot more promising, and since pip3 did work, I assumed that this distutils worked too, and I tried to copy it to the python3.7 folder by running this command:

sudo cp -r /usr/lib/python3.8/distutils /usr/lib/python3.7/distutils

Alternatively:

sudo mv /usr/lib/python3.7/distutils/ /usr/lib/python3.7/distutils_back
sudo ln -s /usr/lib/python3.8/distutils /usr/lib/python3.7/

Then I tried again python3.7 -m pip -V and got

pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.7)

Then I tried installing some modules and everything works fine. I hope this is helpful.

3

I came here for an answer and didn't find, but I fixed it myself.

Some time ago I played with python version by using update-alternatives, also I believe I manually edited some scripts and now, after updating from 16.04 to 20.04, I had the same problem as you. I am not sure if my fix is "proper", yet it works for me:

~$ which pip3
/home/patryk/.local/bin/pip3
~$ vim /home/patryk/.local/bin/pip3

I changed the first line from:

#!/usr/bin/python3.7

to:

#!/usr/bin/python3

Helped in my case.

0

Ensure install appropriate version based on python version, e.g. to get all available versions (assume using apt package manager):

$ apt-cache search distutils
python-setuptools - Python Distutils Enhancements
python-setuptools-doc - Python Distutils Enhancements (documentation)
python3-d2to1 - Python3 support for distutils2-like setup.cfg files as package metadata
python3-setuptools - Python3 Distutils Enhancements
pypy-setuptools - PyPy Distutils Enhancements
python-d2to1 - Python support for distutils2-like setup.cfg files as package metadata
python-distlib - low-level components of python distutils2/packaging
python-distutils-extra - enhancements to the Python build system
python-stsci.distutils - Python packaging utilities for STScI's packages
python3-distlib - low-level components of python distutils2/packaging
python3-distutils-extra - enhancements to the Python3 build system
python3-stdeb - Python to Debian source package conversion plugins for distutils
python3-stsci.distutils - Python3 packaging utilities for STScI's packages
python3.7-distutils - distutils package for Python (version 3.7)
python3.8-distutils - distutils package for Python (version 3.8)
python3.9-distutils - distutils package for Python (version 3.9)

You can see python3.7-distutils, python3.8-distutils, and python3.9-distutils listed in above output, then can install it by specify the version tied to your desired python version, e.g. I have python3.8 and I want to do:

$ sudo apt-get install python3.8-distutils

Then I would run pip with that python version (Ensure you run echo $PYTHONPATH first to confirm python3.8(my python version) in the path otherwise pip will install it in wrong path), e.g.:

$ python3.8 -m pip install requests
3

After install Python3.10 in Ubuntu using ppa:deadsnakes/ppa, I've solved this error executing sudo apt install python3.10-distutils.

Make sure to replace 3.10 which is version of python with appropriate version.

Installing Python3.10

Update system $ sudo apt-get update -y && sudo apt-get full-upgrade -y
Helps to add ppa repositories $ sudo apt install software-properties-common -y
Adding deadsnake repository $ sudo apt-add-repository ppa:deadsnakes/ppa
Installing python $ sudo apt-get install python3.10

Installing distutils.util

$ sudo apt-get install python3.10-distutils

Still using python3.10 -m pip some_command might result in error to fix it use

$ curl -sS | python3.10

I installed alternate versions of python from the deadsnakes PPA. I had to install the versioned distutils for all alternate versions of the form python3.x-distutils. So for python 3.9, I did sudo apt install python3.9-distutils.

1

Don't know if this will help but for me I was trying to install a package using:

$ pip3 install <package>

And that gave the same error, however when I used sudo:

$ sudo pip3 install <package>

it succeeded. I can only imagine that dist-utils are installed for the root user only.

5

I had the same problems as the rest after upgrading to latest version of all packages.

In my case it worked to replace old python default binary with a newer one:

sudo unlink /usr/bin/python
sudo ln -s /usr/bin/python3.8 /usr/bin/python

No need for anything else than those two rows. Uncertain how ubuntu feels about it but its nothing more than switching back now when i can install the package i wanted.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like