[Windows][Python] Fixing Corrupted Pip File

This has happened to me a couple of times in the last few months now. Not sure if it is something to do with the Enterprise Windows 10 VDI I'm using or not. I go to run the simple:

C:\>python -m pip install -r requirements.txt

And get a load of errors.

When this happens, the first thing I do is test whether pip upgrade is working:

Z:\>python -m pip install --upgrade pip

Traceback (most recent call last):

  File "C:\SWDTOOLS\PYTHON39\lib\runpy.py", line 197, in _run_module_as_main

    return _run_code(code, main_globals, None,

  ...

ImportError: cannot import name 'InvalidSchemeCombination' from 'pip._internal.exceptions' (C:\SWDTOOLS\PYTHON39\lib\site-packages\pip\_internal\exceptions.py)

If you can't do a simple pip upgrade, you know something is wrong with pip.

I found the fix on: https://stackoverflow.com/questions/67273590/pip-21-1-cant-import-invalidschemecombination

The reason behind the errors is that the pip file is corrupted.

The solution is as simple as follows:

  1. First execute the command: python -m ensurepip --default-pip
  2. Download the get-pip.py script file from https://bootstrap.pypa.io/get-pip.py
  3. Open a terminal/command prompt (Admin privileges), cd to the folder containing the downloaded get-pip.py file and run: python get-pip.py
  4. The problem has now been resolved.

And it works nice!

C:\>python get-pip.py
Looking in indexes: ~~~
Collecting pip
  Downloading ~~~ (2.1 MB)
Collecting wheel
  Downloading ~~~(35 kB)
Installing collected packages: wheel, pip
  Attempting uninstall: pip
    Found existing installation: pip 22.0.4
    Uninstalling pip-22.0.4:
      Successfully uninstalled pip-22.0.4
Successfully installed pip-22.1.1 wheel-0.37.1

C:\>python -m pip install --upgrade pip
Looking in indexes: ~~~
Requirement already satisfied: pip in c:\~~~\python39\lib\site-packages (22.1.1)

Comments