import sys import subprocess import urllib.request def main(): try: import pip # noqa: F401 print("[+] pip is already installed.") return except ImportError: pass try: import ensurepip ensurepip.bootstrap() print("[+] Successfully bootstrapped pip via ensurepip.") return except Exception as e: print(f"[*] ensurepip bootstrap skipped ({e}), downloading get-pip.py...") ver = f"{sys.version_info.major}.{sys.version_info.minor}" url = f"https://bootstrap.pypa.io/pip/{ver}/get-pip.py" if sys.version_info < (3, 10) else "https://bootstrap.pypa.io/get-pip.py" dest = "/tmp/get-pip.py" if sys.platform != "win32" else "get-pip.py" print(f"[*] Downloading get-pip.py from {url} for Python {ver}...") try: urllib.request.urlretrieve(url, dest) cmd = [sys.executable, dest] if sys.version_info >= (3, 11): cmd.append("--break-system-packages") res = subprocess.run(cmd) if res.returncode == 0: print("[+] Successfully installed pip via get-pip.py") else: # Retry without break-system-packages in case old pip version rejected it subprocess.run([sys.executable, dest]) except Exception as ex: print(f"[!] Warning during pip bootstrap: {ex}") if __name__ == "__main__": main()