219

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.

Rock Life
  • 2,290

19 Answers19

220

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 
jeanollion
  • 2,301
193

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
RobHimself
  • 2,141
26

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.

16

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
ankostis
  • 255
林果皞
  • 596
15

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.

Patryk
  • 251
7

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

3

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.

3

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
3

distutils package is removed in python version 3.12

It was deprecated in Python 3.10 by PEP 632 “Deprecate distutils module”. For projects still using distutils and cannot be updated to something else, the setuptools project can be installed: it still provides distutils.

https://docs.python.org/3.12/whatsnew/3.12.html

Yes, you have to do

pip install setuptools

in fresh virtual environment.

2

Simple answer-

sudo apt-get install python3-distutils

Now, if you have different python versions installed, then the specific version for which you are getting this error(let's say python3.8), then use-

sudo apt-get install python3.8-distutils

A generic answer-

sudo apt-get install python<version>-distutils
1

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
blu3r4y
  • 11
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
superqwerty
  • 165
  • 1
  • 1
  • 7
1

ModuleNotFoundError: No module named 'distutils.cmd'

I have got the above error.. It can be solved by following command

sudo apt-get install python3.8-distutils

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.

PJeffes
  • 101
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/
mrmuggles
  • 101
0

I had the same problem trying to install the pandas package through PyCharm IDE in Ubuntu 22.04. I solved the issue by changing the interpreter in SettingsProjectPython interpreter to conda.

For installing conda, just follow the instructions in the Anaconda website. In fact, you just need to download the shell script, then use chmod +x FILENAME.sh, then run the executable with ./FILENAME.sh. Add the interpreter in PyCharm by selecting the path to the executable.

Hope this helps someone.

0

In my case, I switched from python3.6 into python3.9 and it worked.

0

I needed to do sudo apt install python3.10-venv.

$ python3 -m venv venv1
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.10-venv

You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment.

Failing command: /home/mono/Sachen/CodeLibrary/Python/2024October5/XOrithm Take Home Assignment/venv1/bin/python3

$ sudo apt install python3.10-venv

842Mono
  • 10,070