Last updated: Apr 10, 2024
Reading timeΒ·3 min
The "ImportError: cannot import name 'html5lib' from 'pip._vendor'" occurs
when we have an outdated version of pip
, most commonly on Ubuntu machines that
use Python v3.10.
To solve the error, upgrade your version of pip
.
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.10/dist-packages/pip/_vendor/__init__.py)
pip
.curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10 curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3.10 curl -sS https://bootstrap.pypa.io/get-pip.py | python3 curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3
You can check your Python version with the python --version
command to pipe
the command to the correct Python version.
python --version python3 --version
If that didn't help, try running the following commands to install pip
.
# ποΈ On Linux or MacOS python -m ensurepip --upgrade # ποΈ Using python 3 python3 -m ensurepip --upgrade # ποΈ On Windows py -m ensurepip --upgrade
The ensurepip
package enables us to bootstrap the pip
installer into an
existing Python installation or virtual environment.
If the PATH for pip
is not set up on your machine, replace pip
with
python3 -m pip
:
# ποΈ Make sure to use your version of Python, e.g. 3.10 python3 -m pip install requests python3.10 -m pip install requests
Alternatively, you can use the official get-pip script to install pip.
Download the script from https://bootstrap.pypa.io/get-pip.py by clicking on the link, right-clicking and selecting "Save as" in your browser.
get-pip.py
file is downloaded and run the following command.# ποΈ On Linux or MacOS python get-pip.py # ποΈ Using python 3 python3 get-pip.py # ποΈ On Windows py get-pip.py
The get-pip.py
script uses bootstrapping logic to install pip
.
If none of the suggestions helped, try to install pip
with a command that's
specific to your operating system.
# ποΈ On Debian/Ubuntu sudo apt update sudo apt install python3-venv python3-pip # ποΈ On MacOS brew install python # ποΈ On Fedora/CentOS sudo dnf install python3-pip python3-wheel
Try upgrading pip
by running:
# ποΈ On MacOS or Linux python -m pip install --upgrade pip # ποΈ For Python 3 python3 -m pip install --upgrade pip # ποΈ On Windows py -m pip install --upgrade pip
pip
from the directory where the pip.exe
file is located.First, locate Python by running the following command using cmd
:
# ποΈ For Windows where python # ποΈ Or a specific version if you have multiple installed where python3
Now open the Scripts
folder and make sure it contains the pip.exe
file.
Open your command prompt in the Scripts
directory right next to the pip.exe
file and run the following command.
pip install pip py -m pip install --upgrade pip
If the error persists, try creating a virtual environment.
Another thing that might help is to create a virtual environment if you don't already have one.
# ποΈ Use the correct version of Python when creating VENV python3 -m venv venv # ποΈ Activate on Unix or MacOS source venv/bin/activate # ποΈ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ποΈ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ποΈ Upgrade pip pip install --upgrade pip # ποΈ Install <package-name> in your virtual environment pip install <package-name>
If the python3 -m venv venv
command doesn't work, try the following 2
commands:
python -m venv venv
py -m venv venv
Make sure to use the correct activation command depending on your operating system.
Your virtual environment will use the version of Python that was used to create it.
If the error persists, follow the instructions in my ModuleNotFoundError: No module named 'pip' article.