def pip_import_string()

in aws_lambda_builders/workflows/python_pip/compat.py [0:0]


def pip_import_string(python_exe):
    os_utils = OSUtils()
    cmd = [python_exe, "-c", "import pip; print(pip.__version__)"]
    p = os_utils.popen(cmd, stdout=os_utils.pipe, stderr=os_utils.pipe, env=os_utils.original_environ())
    stdout, stderr = p.communicate()
    if not p.returncode == 0:
        raise MissingPipError(python_path=python_exe)
    pip_version = stdout.decode("utf-8").strip()
    pip_major_version = int(pip_version.split(".")[0])
    pip_minor_version = int(pip_version.split(".")[1])

    # Pip moved its internals to an _internal module in version 10.
    # In order to be compatible with version 9 which has it at at the
    # top level we need to figure out the correct import path here.
    pip_version_9 = 9
    if pip_major_version == pip_version_9:
        return "from pip import main"
    # Pip changed their import structure again in 19.3
    # https://github.com/pypa/pip/commit/09fd200
    elif (pip_major_version, pip_minor_version) >= (19, 3):
        return "from pip._internal.main import main"
    else:
        return "from pip._internal import main"