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
.
The module not found likely means the packages aren't installed.
If they're already installed you can try to fix anything that may have been messed up in the upgrade with...
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:
And then installed the
distutils
package for Python 3.7:Note: for some reason I had an error installing the latter, that I solved this way:
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 folderdistutil
.I opened it, and saw the
__pycache__
, the__init__.py
file and aversion.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: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:
Then I tried again
python3.7 -m pip -V
and gotpip 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.
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:
I changed the first line from:
to:
Helped in my case.
Ensure install appropriate version based on python version, e.g. to get all available versions (assume using
apt
package manager):You can see
python3.7-distutils
,python3.8-distutils
, andpython3.9-distutils
listed in above output, then can install it by specify the version tied to your desired python version, e.g. I havepython3.8
and I want to do: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.:After install Python3.10 in Ubuntu using
ppa:deadsnakes/ppa
, I've solved this error executingsudo apt install python3.10-distutils
.Make sure to replace
3.10
which is version ofpython
with appropriate version.Installing Python3.10
Installing
distutils.util
Still using
python3.10 -m pip some_command
might result in error to fix it useI 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 didsudo apt install python3.9-distutils
.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
, sopip
doesn't works with python3.9 (which is bad because you can't uninstall anything installed withpython3.9
earlier).So use:
python3.10 -m pip install
it works.And you can make pip use of python3.10 by default like this:
Don't know if this will help but for me I was trying to install a package using:
And that gave the same error, however when I used sudo:
it succeeded. I can only imagine that dist-utils are installed for the root user only.