21

I updated python to python3.9, and when I try to install pip, I get an error:

lors@Lenovo:~$ pip3 install pipenv
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 29, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/usr/lib/python3/dist-packages/pip/utils/__init__.py", line 23, in <module>
    from pip.locations import (
  File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in <module>
    from distutils import sysconfig
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.9/distutils/__init__.py)
1
  • I have the same problem on Ubuntu 18.04 having installed Python 3.9 off the deadsnakes PPA
    – Pankrates
    Dec 2, 2020 at 13:19

2 Answers 2

53

I am assuming you did changed update-alternatives for python3. What is happening that you have distutils installed in your last python version, but not in the new one! And because you've changed system link with "update-alternatives", system no longer finds it with the new link of python3. To fix this you have to install distutils to python3.9.

sudo apt install python3.9-distutils

And then you can test it with following line written in terminal:

python3 -c "from distutils import sysconfig"

If there is no Traceback you are good to go.

4
  • 1
    thanks for your answer, but i reinstall my linux Dec 4, 2020 at 0:08
  • In future avoid changing system links. Why I recommend this? You will have less headaches if you are new to linux. Better for you just use /path/to/python. Be extremely careful using update-alternatives.
    – ikansazov
    Dec 6, 2020 at 17:54
  • thank for your attention dude, i dont have idea how i got this error and yes i am new in linux;) Dec 7, 2020 at 20:49
  • 1
    for the world I can't even begin to imagine how you thought of that, thanks! Apr 9, 2022 at 20:15
2

Answer(work in progress,collaboratively): upon review of the locations.py file referenced in the PyCharm error message('Failed to install package Kivy')

File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in <module>
    from distutils import sysconfig
(most recent traceback: 'ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.9/distutils/__init__.py)'

at line 76 in locations.py, there is a block that reads as following:

# under macOS + virtualenv sys.prefix is not properly resolved
# it is something like /path/to/python/bin/..
# Note: using realpath due to tmp dirs on OSX being symlinks
src_prefix = os.path.abspath(src_prefix)

# FIXME doesn't account for venv linked to global site-packages

this is an answer in progress but our problem seems to be referential to an inconsistent sys.prefix and/or src_prefix value defined by locations.py, troubleshooting and back-tracing now, will update shortly. Edit/update: at line 82 i believe is the problem,

# FIXME doesn't account for venv linked to global site-packages

but a lot of this is over my head, i'm not equipped to re-write pip scripts at present. the distutils directory it's linking to '/usr/lib/python3.9/distutils/' is effectively empty, and missing most of the files it needs to carry through with the installation, but i'm unsure as of yet how to change the src_prefix value in-place to a better link to the working directory for python3, as distutils was deprecated post-3.2, i believe. linking to the base python3 install might be the best bet.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .