Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python error - "ImportError: cannot import name 'dist'"

Tags:

python

linux

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?

like image 394
omer mazig Avatar asked May 06 '18 14:05

omer mazig


3 Answers

try it

sudo apt install python3-distutils
like image 64
user1941407 Avatar answered Oct 29 '22 04:10

user1941407


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

like image 24
Vadim Avatar answered Oct 29 '22 04:10

Vadim


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
like image 18
user2314737 Avatar answered Oct 29 '22 04:10

user2314737