Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/utils/entrypoints.py
4804 views
import itertools1import os2import shutil3import sys4from typing import List, Optional56from pip._internal.cli.main import main7from pip._internal.utils.compat import WINDOWS89_EXECUTABLE_NAMES = [10"pip",11f"pip{sys.version_info.major}",12f"pip{sys.version_info.major}.{sys.version_info.minor}",13]14if WINDOWS:15_allowed_extensions = {"", ".exe"}16_EXECUTABLE_NAMES = [17"".join(parts)18for parts in itertools.product(_EXECUTABLE_NAMES, _allowed_extensions)19]202122def _wrapper(args: Optional[List[str]] = None) -> int:23"""Central wrapper for all old entrypoints.2425Historically pip has had several entrypoints defined. Because of issues26arising from PATH, sys.path, multiple Pythons, their interactions, and most27of them having a pip installed, users suffer every time an entrypoint gets28moved.2930To alleviate this pain, and provide a mechanism for warning users and31directing them to an appropriate place for help, we now define all of32our old entrypoints as wrappers for the current one.33"""34sys.stderr.write(35"WARNING: pip is being invoked by an old script wrapper. This will "36"fail in a future version of pip.\n"37"Please see https://github.com/pypa/pip/issues/5599 for advice on "38"fixing the underlying issue.\n"39"To avoid this problem you can invoke Python with '-m pip' instead of "40"running pip directly.\n"41)42return main(args)434445def get_best_invocation_for_this_pip() -> str:46"""Try to figure out the best way to invoke pip in the current environment."""47binary_directory = "Scripts" if WINDOWS else "bin"48binary_prefix = os.path.join(sys.prefix, binary_directory)4950# Try to use pip[X[.Y]] names, if those executables for this environment are51# the first on PATH with that name.52path_parts = os.path.normcase(os.environ.get("PATH", "")).split(os.pathsep)53exe_are_in_PATH = os.path.normcase(binary_prefix) in path_parts54if exe_are_in_PATH:55for exe_name in _EXECUTABLE_NAMES:56found_executable = shutil.which(exe_name)57if found_executable and os.path.samefile(58found_executable,59os.path.join(binary_prefix, exe_name),60):61return exe_name6263# Use the `-m` invocation, if there's no "nice" invocation.64return f"{get_best_invocation_for_this_python()} -m pip"656667def get_best_invocation_for_this_python() -> str:68"""Try to figure out the best way to invoke the current Python."""69exe = sys.executable70exe_name = os.path.basename(exe)7172# Try to use the basename, if it's the first executable.73found_executable = shutil.which(exe_name)74if found_executable and os.path.samefile(found_executable, exe):75return exe_name7677# Use the full executable name, because we couldn't find something simpler.78return exe798081