44

after updating to ubuntu 20.04 whenever i type "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
60

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
8
  • 4
    i've already tried this doesnt work – Rock Life May 15 '20 at 17:35
  • I notice python 3.7 listed in those messages. Ubuntu 20.04 uses python 3.8.2 as default. What does python3 --version return? – Viginti May 15 '20 at 19:45
  • it says Python 3.8.2 – Rock Life May 15 '20 at 20:05
  • 1
    Turns out distutils was simply missing from my system. +1 – Soutzikevich Jun 29 '20 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 at 22:07
11

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/distutil /usr/lib/python3.7/distutil

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.

2
  • 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 at 22:10
  • cp -r /usr/lib/python3.9/distutils /usr/lib/python3.7/distutils this worked for me. – Petr Javorik Mar 16 at 14:11
8

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.

1
  • This really worked! Thanks so much, I didn't need to do any of the other suggestions, this fixed my problem instantly. – Geoff Davids Jan 22 at 21:16
5

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 
1
  • Thanks for the tip, all I needed to do was to run sudo apt install python3.9-distutils – jgosar Feb 3 at 6:34
2

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
  • 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 '20 at 0:01
  • 2
    @levitopher I think it changed recently. – Fruit Jun 25 '20 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 at 22:18
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? – connelblaze Sep 29 '20 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 '20 at 6:36
  • using sudo is not really the best way anyway, but all good – connelblaze Oct 12 '20 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 '20 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 at 21:06

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.