141

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

14 Answers 14

143

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
11
  • 16
    i've already tried this doesnt work
    – Rock Life
    May 15, 2020 at 17:35
  • 1
    Turns out distutils was simply missing from my system. +1 Jun 29, 2020 at 12:09
  • 1
    @rock-life could it be've been bitten by debian's bug #979819 which kicks in when you want to install an older Python version than the one your Ubuntu/Debian comes with? Check this workaround.
    – ankostis
    Jan 21, 2021 at 22:07
  • 1
    thanks, it worked for me today. Jul 1, 2021 at 4:57
  • 1
    Thank you, it helps me. Solved the problem
    – Guilherme
    Jun 2 at 16:50
113

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 
5
  • 15
    Thanks for the tip, all I needed to do was to run sudo apt install python3.9-distutils
    – jgosar
    Feb 3, 2021 at 6:34
  • 4
    Yea thanks, I needed that for python3.10 --> sudo apt install python3.10-distutils
    – Ali Rn
    Feb 25 at 8:35
  • 2
    I upgraded from ubuntu 20.04 -> 22. This fixed my issues
    – S.MC.
    May 30 at 13:31
  • Thank you, your solution works to me
    – Guilherme
    Jul 5 at 18:56
  • I also needed to install python3.7-dev Jul 8 at 9:47
22

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.

4
  • Actually a symlink would suffice: sudo rm -rf /usr/lib/python3.8/distutil && sudo ln -s /usr/lib/python3.8/distutil /usr/lib/python3.7/distutil BUT for really complicated combinations, read the notes at the bottom of this workaround.
    – ankostis
    Jan 21, 2021 at 22:10
  • cp -r /usr/lib/python3.9/distutils /usr/lib/python3.7/distutils this worked for me. Mar 16, 2021 at 14:11
  • This was the only solution that worked when I had to switch Python version from 3.9 to 3.8 on Raspberry Pi OS and make pip3 and virtualenvs work. Mar 21 at 10:10
  • I had a similar issue, but opposite: I had Python 3.8 and 3.6. The 3.6 distutils folder had the code, but nothing in 3.8. I copied the 'package' to Python 3.8 and now it works properly.
    – mrmuggles
    Jun 29 at 14:55
12

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
8

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
4
  • 1
    Not a good solution - on my system that command just spits out "python3-distutils - distutils package for Python 3.x", it's not broken up by sub-versions.
    – levitopher
    Jun 25, 2020 at 0:01
  • 2
    @levitopher I think it changed recently.
    – 林果皞
    Jun 25, 2020 at 11:08
  • @levitopher your system is it should; it seems Fruit does not delete old apt-repos from previous Ubuntu releases after upgrade, and that's why he got all these results. You can still download those from the ubuntu-site. Also check the explanation of note-2 at the bottom of this SO response.
    – ankostis
    Jan 21, 2021 at 22:18
  • if you got a error Unable to locate package python3.8-distutils, run sudo add-apt-repository ppa:deadsnakes/ppa to fix it.
    – vladimir
    Aug 16 at 23:47
6

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

5

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 https://bootstrap.pypa.io/get-pip.py | python3.10
2

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
1

Get the same after upgrade from 20.04 to 22.04

22.04 comes with python3.10.
And after upgrade python3.9 still exist beside 3.10 version, but has not distutils, so pip doesn't works with python3.9 (which is bad because you can't uninstall anything installed with python3.9 earlier).

So use: python3.10 -m pip install it works.

And you can make pip use of python3.10 by default like this:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 4
0

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
  • does pip3 install <some package> work? Sep 29, 2020 at 20:53
  • I don't mean to be rude but I don't understand what you mean. The first part of my response explains that using what you have written results in an error, which makes me think I might have missed something about your question.
    – PJeffes
    Oct 12, 2020 at 6:36
  • using sudo is not really the best way anyway, but all good Oct 12, 2020 at 22:15
  • I agree with that and normally wouldn't use it - however I posted this as a quick fix. It just seems that something in the config makes dist-utils only available to the root user, which I agree is an issue, but if one just wants to install the package then running with sudo is a quick fix. If you think this is dangerous or strongly against best practice then let me know and I'll remove.
    – PJeffes
    Oct 15, 2020 at 10:38
  • When running system pip, it tries to install stuff in the /usr/local/{bin,share/python} directories, that are root-only writable. Actually you are expanding your system's python installation with the requested packages, which may masquerade python packages install with apt. To avoid that, work with virtual-envs.
    – ankostis
    Jan 22, 2021 at 21:06
0

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.

0

I had this issue with Python 3.8 - Ubuntu 18.04 when running python3.8 -m venv blcorp:

ModuleNotFoundError: No module named 'distutils.util'

The fix was similar to Ciro's answer (https://askubuntu.com/a/1260519/1608427):

I had a 3.7 version of distutils, I copied it in the python3.8 distutils package and no more error.

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

I had a similar issue with the packages from ppa:deadsnakes/ppa on Ubuntu 20.04 and resolved it by installing python3.10-full. Complete instructions:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.10-full
0

I encountered this issue as I upgraded my system from Python 3.9 to Python 3.10 but my terminal (within Pycharm) automatically loaded the venv from Python 3.9 - referencing outdated/missing packages because the upgrade process installed the 3.10 versions of those packages.

Recreating my venv did the trick:

rm -r venv
python3 -m venv venv
source ./venv/bin/activate
pip3 install -r requirements.txt

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.