Pip is not working for Python 3.10 on Ubuntu

I am new to using Ubuntu and Linux in general. I just attempted to update Python by using sudo apt-get install python3.10. When I run python3.10 -m pip install <library name> I always receive the following error:

Traceback (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/lib/python3/dist-packages/pip/__main__.py", line 19, in <module>
sys.exit(_main())
File "/usr/lib/python3/dist-packages/pip/_internal/cli/main.py", line 73, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "/usr/lib/python3/dist-packages/pip/_internal/commands/__init__.py", line 96, in create_command
module = importlib.import_module(module_path)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py", line 24, in <module>
from pip._internal.cli.req_command import RequirementCommand
File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 15, in <module>
from pip._internal.index.package_finder import PackageFinder
File "/usr/lib/python3/dist-packages/pip/_internal/index/package_finder.py", line 21, in <module>
from pip._internal.index.collector import parse_links
File "/usr/lib/python3/dist-packages/pip/_internal/index/collector.py", line 12, in <module>
from pip._vendor import html5lib, requests
ImportError: cannot import name 'html5lib' from 'pip._vendor' (/usr/lib/python3/dist-packages/pip/_vendor/__init__.py)```

I have tried doing sudo apt-get install python3.10-html5lib it downloaded something but it did not fix the error. How should I fix this error?

71800 次浏览

At the time of writing and as stated on the following link. Python 3.10 is still unstable. Maybe it is a good idea to test what happens with python3.9 and see if the issue is in your local installation.
in any case, from a clean ubuntu (docker) installation by running (with sudo rights)

apt-get install software-properties-common
apt-get install python3.9
python3.9 --version
#
apt-get install python3.10
python3.10 --version

I got an the answers Python 3.9.5 and bash: python3.10: command not found respectively. so I guess the issue could be in the python 3.10 repository... for now.

However, apt-get install will not update directly your "default" python. You can check which are your those installations with

python --version
python3 --version

Any way, if you still want to install python3.10, perhaps the link above could help you.

This is likely caused by a too old system pip version.

Install the latest with:
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10

and test result

python3.10 -m pip --version

e.g.

pip 22.2.2 from <home>/.local/lib/python3.10/site-packages/pip (python 3.10)

and then test upgrade

python3.10 -m pip install --upgrade pip

e.g.

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in <home>/.local/lib/python3.10/site-packages (22.2.2)

Try:

pip3.10 install <package name>

Make sure you have the latest version pip and python3.10

I've got the same error after installing python 3.10 on Ubuntu with python 3.8 on board. uninstall pip and install it again

sudo apt-get remove -y python3-pip
sudo apt-get install -y python3-pip

it's worked for me, but pip3.10 install modulename got the same error, and python3.10 -m pip install modulename working fine.

I had some issues installing pip as well

As sudo apt install python3-pip is still refering to pip3.8 (on mint Cinnamon 20.2 at least), pip needs to be installed in a other way.

With curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10, it turns out that pip is not found

ImportError: No module named 'pip._internal'

I found out that ensurepip command was built in and target the correct directory for python3.10

Running python3.10 -m ensurepip installs setuptools and pip and fix my missing pip module.

Pip can be updgraded afterwards using python3.10 -m pip install -U pip.

It also work with sudo (had some needs running pip as sudo)

The code for installing python and pip 3.10 for Ubuntu is all there, it's enough to swap just one line of that code, changing base image to the latest Ubuntu LTS version (that's ubuntu:focal) in the Dockerfile for the official Debian-based python 3.10 image (I used python 3.10 bullseye slim Dockerfile. Docker build took just 8 minutes, and here is proof that pip3.10 really works there:

$ docker run mirekphd/python3.10-ubuntu20.04 pip -V

Output:

pip 21.3.1 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)

Or a more advanced example:

$ docker run -it --rm --name python3.10-ubuntu20.04 mirekphd/python3.10-ubuntu20.04:latest bash -c 'pip --version; pip install numpy --user --no-cache; pip show numpy; python -c "import numpy as np; print(np.ones(5))"'

Output:

pip 21.3.1 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
Collecting numpy
Downloading numpy-1.21.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.9 MB)
|████████████████████████████████| 15.9 MB 34.6 MB/s
Installing collected packages: numpy
WARNING: The scripts f2py, f2py3 and f2py3.10 are installed in '/root/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.21.4
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Name: numpy
Version: 1.21.4
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License: BSD
Location: /root/.local/lib/python3.10/site-packages
Requires:
Required-by:
[1. 1. 1. 1. 1.]

Note: to upgrade pip, I just added this line (before CMD):

RUN python -m pip install --upgrade pip

This is an expanded version of @mirekphd's comment which gave me a solution that worked both on my laptop and on our server perfectly without messing anything up.

First step, if not already done is to add the 'deadsnakes' repository so that the latest python related software can be downloaded and kept updated.

sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa

And, if you don't have Python 3.10 already installed you can do that with:

sudo apt install python3.10

And finally, to make pip work you will need to install the distutils package that's compatible with 3.10:

apt install python3.10-distutils

After that you can use pip the following way:

python3.10 -m pip _rest_of_the_pip

The above is necessary because the regular pip command will utilize the systems default python version and not 3.10.

If you use pip frequently then you can add an alias to your .bashrc file such as:

alias pip310='python3.10 -m pip'

After that you can just use it like this:

pip310 install blah

I've had this problem inside GitHub Actions. My solution was to resort to virtual environments to isolate from the rest of the system. Ensure python3.10-venv is installed and then do

python3.10 -m venv .venv
source .venv/bin/activate
pip install ...
...
deactivate

I have tried all the answers above but nothing worked for me. My python3 version is 3.10.2.

So I need to install the appropriate version for pip.

However, I have tried

curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10

But didn't work so, I tried

curl https://bootstrap.pypa.io/get-pip.py | sudo python3

This worked for me

in my case this problem is associated to a previous conda installation... so I solve it by correcting the PATH. Since also Python3.10 & pip3 are installed in ~/.local/bin

In the worst way, I added a last line to ~/.bashrc hardcoded as follows

nano ~/.bashrc

add the last line:

export PATH=/usr/local/sbin:/sbin:/usr/sbin:/root/bin:/usr/local/bin:/usr/bin:/usr/local/games:/usr/games:/home/$USER/.local/bin

save, close and run

source .bashrc

this was not enough, because after removing Conda's paths it still left pip installed in /usr/bin I solved this by renaming this pip

cd /usr/bin
sudo mv pip kkpip
sudo mv pip3 kkpip3

done

pip3 list

Voilà!