37

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?

omer mazig
  • 965
  • 2
  • 10
  • 17

6 Answers6

50

try it

sudo apt install python3-distutils
user1941407
  • 2,722
  • 4
  • 27
  • 39
  • 2
    it fixed the ImportError for me (Ubuntu 16.04 inside a virtualenv with python3.6). – jfs May 14 '18 at 08:56
  • there's no such thing as Ubuntu inside a virtualenv. You must mean virtualenv with python3.6 running on Ubuntu 16.04 – cryanbhu Jul 15 '18 at 07:50
  • 11
    "python3-distutils is already the newest version (3.7.3-1ubuntu1)" did not solve problem (which happens in a virtualenv, not system python) – Gringo Suave May 21 '19 at 20:05
24

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

Vadim
  • 1,131
  • 17
  • 16
  • 12
    yep, i just upgraded from 19.10 to 20.04 and the same happened to me. – Comm4nd0 May 10 '20 at 15:41
  • 1
    @Comm4nd0 Same here. But I could not create a requirements.txt from the old one either! Should be done before the Ubuntu upgrade! – smcs Sep 08 '20 at 12:03
18

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
user2314737
  • 27,088
  • 20
  • 102
  • 114
  • 3
    But if `python-3.7` remains installed in the system, it will still be broken, and cannot import the missing `distutils/dist.py` file :-( – ankostis Aug 26 '20 at 09:29
  • $ sudo apt-get install -y python3.8-distutils worked. – m9m9m Nov 04 '20 at 06:25
13

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.

Thibault J
  • 4,336
  • 33
  • 44
0

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.

-1

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.

  • It's not ideal to compile things from source in Ubuntu, regarding updates and distribution integration. – Herbert Jun 26 '20 at 15:01