ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.9/distutils/__init__.py)

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)

Solution 1:

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.