I’m on Ubuntu 16.04, and I get:

Traceback (most recent call last):
  File "/home/omermazig/.virtualenvs/fixi/bin/pip", line 7, in <module>
    from pip import main
  File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/site-packages/pip/utils/__init__.py", line 23, in <module>
    from pip.locations import (
  File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/site-packages/pip/locations.py", line 9, in <module>
    from distutils import sysconfig
  File "/home/omermazig/.virtualenvs/fixi/lib/python3.6/distutils/__init__.py", line 25, in <module>
    from distutils import dist, sysconfig
ImportError: cannot import name 'dist'

When I run anything with python. This specifically is for trying to run “pip freeze”. What to do?

try it

sudo apt install python3-distutils

My case was when I upgraded Ubuntu 18 -> 19.
So it reinstalled python and what I needed to do is:

  1. remove old virtual environment

  2. create a new one

  3. install requirements via pip into it

I ran into this problem after installing Python 3.8 on Ubuntu (I have version 16.04)

$ lsb_release -d
Description:     Ubuntu 16.04.6 LTS
  1. Reproduce the error

Just try to import distutils

$ python3 -c "from distutils import sysconfig"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.8/distutils/__init__.py)

$ sudo apt install python3-distutils          
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python3-distutils

The package python3-distutils cannot be found

$ sudo apt install python3-distutils          
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python3-distutils
  1. list available distutils

What helped was to list all distutil packages using a regexp

$ apt list *distutils*
Listing... Done
python-distutils-extra/xenial,xenial 2.39-1 all
python-stsci.distutils/xenial,xenial 0.3.7-4 all
python3-distutils-extra/xenial,xenial 2.39-1 all
python3-stsci.distutils/xenial,xenial 0.3.7-4 all
python3.7-distutils/xenial,xenial 3.7.8-1+xenial1 all
python3.8-distutils/xenial,xenial 3.8.3-1+xenial1 all
python3.9-distutils/xenial,xenial 3.9.0~b4-1+xenial1 all
  1. Install the correct distutils package

For my Python 3.8 I picked python3.8-distutils and it worked

$ sudo apt-get install -y python3.8-distutils

Since I run into this issue everytime I update my ubuntu version every six months, then stumble on the exact same SO result, here is my solution.

If the other solutions listed here don’t work (installing python3-distutils), it might be because of different python versions between the system and the virtualenv.

The easy solution is to destroy your virtualenv, then recreate it from scratch.

Solved: I’ve just got this issue in a virtual environment installed 2 years ago, using python3.7

Running pip3 or python3.7 -c “from distutils import dist, sysconfig” from the venv, I got the error ImportError: cannot import name ‘dist’

Using system python 3.9, this error disappers.

I solved copying the /usr/lib/python3.9/distutils into the python3.7 virtual environment.

Take a loot at this (similar problem):
https://github.com/pypa/pip/issues/5367

Possible fix:

  • Download Python source from https://www.python.org/
  • Decompress the source code
  • Install the following dependencies:

    sudo apt-get install zlib1g-dev (needed to compile Python)

  • and install:

sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev (needed by Pip to make SSL requests)

  • Compile and install Python:

/configure

make

make install

  • Python 3.6 with Pip should be installed.

Full credit to jonbesga.