__pip-runner__.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Execute exactly this copy of pip, within a different environment.
  2. This file is named as it is, to ensure that this module can't be imported via
  3. an import statement.
  4. """
  5. import runpy
  6. import sys
  7. import types
  8. from importlib.machinery import ModuleSpec, PathFinder
  9. from os.path import dirname
  10. from typing import Optional, Sequence, Union
  11. PIP_SOURCES_ROOT = dirname(dirname(__file__))
  12. class PipImportRedirectingFinder:
  13. @classmethod
  14. def find_spec(
  15. self,
  16. fullname: str,
  17. path: Optional[Sequence[Union[bytes, str]]] = None,
  18. target: Optional[types.ModuleType] = None,
  19. ) -> Optional[ModuleSpec]:
  20. if fullname != "pip":
  21. return None
  22. spec = PathFinder.find_spec(fullname, [PIP_SOURCES_ROOT], target)
  23. assert spec, (PIP_SOURCES_ROOT, fullname)
  24. return spec
  25. sys.meta_path.insert(0, PipImportRedirectingFinder())
  26. assert __name__ == "__main__", "Cannot run __pip-runner__.py as a non-main module"
  27. runpy.run_module("pip", run_name="__main__", alter_sys=True)