ImportError: cannot import name 'html5lib' from pip._vendor

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# ImportError: cannot import name 'html5lib' from pip._vendor

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.

shell
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)
The error is most often caused on Ubuntu when using Python v3.10. If you are on Ubuntu, try the following 4 commands to upgrade your version of pip.
shell
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

ubuntu upgrade pip

You can check your Python version with the python --version command to pipe the command to the correct Python version.

shell
python --version python3 --version

get python version

# Using ensurepip

If that didn't help, try running the following commands to install pip.

shell
# πŸ‘‡οΈ On Linux or MacOS python -m ensurepip --upgrade # πŸ‘‡οΈ Using python 3 python3 -m ensurepip --upgrade # πŸ‘‡οΈ On Windows py -m ensurepip --upgrade

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:

shell
# πŸ‘‡οΈ Make sure to use your version of Python, e.g. 3.10 python3 -m pip install requests python3.10 -m pip install requests

install using correct pip version

# Using the official get-pip script

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.

Open your terminal in the location where the get-pip.py file is downloaded and run the following command.
shell
# πŸ‘‡οΈ On Linux or MacOS python get-pip.py # πŸ‘‡οΈ Using python 3 python3 get-pip.py # πŸ‘‡οΈ On Windows py get-pip.py

python get pip py script

The get-pip.py script uses bootstrapping logic to install pip.

# Installing pip with an os-specific command

If none of the suggestions helped, try to install pip with a command that's specific to your operating system.

shell
# πŸ‘‡οΈ 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:

shell
# πŸ‘‡οΈ 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
If none of the suggestions helped on a Windows machine, try installing pip from the directory where the pip.exe file is located.

First, locate Python by running the following command using cmd:

shell
# πŸ‘‡οΈ 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.

shell
pip install pip py -m pip install --upgrade pip

If the error persists, try creating a virtual environment.

# Try creating a virtual environment

Another thing that might help is to create a virtual environment if you don't already have one.

shell
# πŸ‘‡οΈ 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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.