109

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?

Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
Christopher Coco
  • 1,101
  • 2
  • 3
  • 3
  • 3
    It is important to grasp that when installing `python`, `pip` will not come automatic with it. You have to install the module separately, as seen [in the answer by nnn](https://stackoverflow.com/a/69527217/1705829) – Timo Nov 20 '21 at 17:31
  • I fixed this on a venv with `easy_install -U pip`. – scr Apr 26 '23 at 07:11

11 Answers11

187

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)
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
nnn
  • 3,980
  • 1
  • 13
  • 17
  • 7
    ... and do not follow [the first google search result](https://computingforgeeks.com/how-to-install-python-on-debian-linux/) for installation of pip - `apt install python3-pip`, it installs `python3.8` together with pip on `Ubuntu 20.04` as of today. – Timo Nov 20 '21 at 17:21
  • 31
    On `Ubuntu 20.04` with `python3.10` from `deadsnakes-ubuntu-ppa-focal.list` installed, I get `ModuleNotFoundError: No module named 'distutils.cmd' ` – Timo Nov 20 '21 at 17:23
  • 15
    You can `apt install python${PYTHON_VERSION}-distutils` from `deadsnakes` repo, but tell me please when you succeed installing `numpy` with `pip3.10`. – mirekphd Nov 21 '21 at 19:48
  • @mirekphd thanks for the hint, I will try numpy - a side info: consider to address your comment to me with the "at sign plus user" so that I can be aware of it. – Timo Nov 26 '21 at 07:55
  • These two commands worked for me on Ubuntu18: curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10 && apt-get install -y --no-install-recommends python3-pip – alvaro nortes Jan 18 '22 at 10:18
  • 13
    For those who are wondering how this command works: **curl** is used to download the file from the given link the parameter -sS means **s** (small s) represent **silent** and **S** (capital s) means **show error**. Curl will download the **get-pip.py** file which has some python code and then we pass the code to python3.10 command this way python3.10 will run get-pip.py and this way latest version of pip will be installed – Udesh Feb 03 '22 at 04:55
  • is this a problem only related to Ubuntu 20.04 and python3.10? – mattiatantardini Nov 21 '22 at 08:23
34

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

Yaneeve
  • 4,751
  • 10
  • 49
  • 87
Shyam
  • 547
  • 4
  • 11
14

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
BenVida
  • 1,796
  • 1
  • 16
  • 25
11

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
Phoenix87
  • 1,003
  • 11
  • 15
6

Install python 3.10, pip and venv in Ubuntu 18|20

sudo apt update && sudo apt upgrade -y
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.10
python3.10 --version
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10
python3.10 -m pip --version
python3.10 -m pip install --upgrade pip
sudo apt install python3.10-venv

Create a virtual environment,

python3.10 -m venv venv3.10
source venv3.10/bin/activate
Jerin Antony
  • 133
  • 1
  • 9
2

Try:

pip3.10 install <package name>

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

jpedro
  • 31
  • 1
2

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)

Quentium
  • 335
  • 1
  • 5
  • 16
1

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.

eliasmaxil
  • 530
  • 4
  • 13
  • OP already installed python3.10, perhaps after adding a third party repo like `sudo add-apt-repository ppa:deadsnakes/ppa && sudo apt update` – nnn Oct 11 '21 at 13:51
1

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
mirekphd
  • 4,799
  • 3
  • 38
  • 59
0

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.

0

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à!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253