Skip to content

Instantly share code, notes, and snippets.

@shirakaba
Last active December 29, 2023 08:27
Show Gist options
  • Save shirakaba/b9d30de2b59090cfc1818b7ed7c5256c to your computer and use it in GitHub Desktop.
Save shirakaba/b9d30de2b59090cfc1818b7ed7c5256c to your computer and use it in GitHub Desktop.
Fixing pip when even `pip --version` fails

A very common failure pattern of pip is the following:

> pip --version
/usr/local/bin/pip:6: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  from pkg_resources import load_entry_point
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 631, in _build_master
    ws.require(__requires__)
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 968, in require
    needed = self.resolve(parse_requirements(requirements))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 829, in resolve
    dist = self._resolve_dist(
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 875, in _resolve_dist
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 23.3.1 (/opt/homebrew/lib/python3.11/site-packages), Requirement.parse('pip==23.3.2'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 3327, in <module>
    @_call_aside
     ^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 3302, in _call_aside
    f(*args, **kwargs)
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 3340, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 633, in _build_master
    return cls._build_from_requirements(__requires__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 646, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 829, in resolve
    dist = self._resolve_dist(
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pkg_resources/__init__.py", line 870, in _resolve_dist
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==23.3.2' distribution was not found and is required by the application

To fix, first find pip:

> which pip
< /usr/local/bin/pip

Then ask Python what version pip is because it can't even report it itself:

> python -m pip --version
< pip 23.3.1 from /opt/homebrew/lib/python3.11/site-packages/pip (python 3.11)

Now edit its executable (which is mercifully a shell script) to reference that version of pip:

sudo nano /usr/local/bin/pip
  #!/opt/homebrew/opt/python3/libexec/bin/python
- # EASY-INSTALL-ENTRY-SCRIPT: 'pip==23.3.2','console_scripts','pip'
+ # EASY-INSTALL-ENTRY-SCRIPT: 'pip==23.3.1','console_scripts','pip'
- __requires__ = "pip==23.3.2"
+ __requires__ = "pip==23.3.1"
  import re
  import sys
  from pkg_resources import load_entry_point

  if __name__ == "__main__":
      sys.argv[0] = re.sub(r"(-script\.pyw?|\.exe)?$", "", sys.argv[0])
-     sys.exit(load_entry_point("pip==23.3.2", "console_scripts", "pip")())
+     sys.exit(load_entry_point("pip==23.3.1", "console_scripts", "pip")())

Now that pip can actually open, if you're feeling brave, you can update it and go back to update the version referenced in that script:

python3.11 -m pip install --upgrade pip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment