Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/cli/main.py
4804 views
"""Primary application entrypoint.1"""2import locale3import logging4import os5import sys6from typing import List, Optional78from pip._internal.cli.autocompletion import autocomplete9from pip._internal.cli.main_parser import parse_command10from pip._internal.commands import create_command11from pip._internal.exceptions import PipError12from pip._internal.utils import deprecation1314logger = logging.getLogger(__name__)151617# Do not import and use main() directly! Using it directly is actively18# discouraged by pip's maintainers. The name, location and behavior of19# this function is subject to change, so calling it directly is not20# portable across different pip versions.2122# In addition, running pip in-process is unsupported and unsafe. This is23# elaborated in detail at24# https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program.25# That document also provides suggestions that should work for nearly26# all users that are considering importing and using main() directly.2728# However, we know that certain users will still want to invoke pip29# in-process. If you understand and accept the implications of using pip30# in an unsupported manner, the best approach is to use runpy to avoid31# depending on the exact location of this entry point.3233# The following example shows how to use runpy to invoke pip in that34# case:35#36# sys.argv = ["pip", your, args, here]37# runpy.run_module("pip", run_name="__main__")38#39# Note that this will exit the process after running, unlike a direct40# call to main. As it is not safe to do any processing after calling41# main, this should not be an issue in practice.424344def main(args: Optional[List[str]] = None) -> int:45if args is None:46args = sys.argv[1:]4748# Configure our deprecation warnings to be sent through loggers49deprecation.install_warning_logger()5051autocomplete()5253try:54cmd_name, cmd_args = parse_command(args)55except PipError as exc:56sys.stderr.write(f"ERROR: {exc}")57sys.stderr.write(os.linesep)58sys.exit(1)5960# Needed for locale.getpreferredencoding(False) to work61# in pip._internal.utils.encoding.auto_decode62try:63locale.setlocale(locale.LC_ALL, "")64except locale.Error as e:65# setlocale can apparently crash if locale are uninitialized66logger.debug("Ignoring error %s when setting locale", e)67command = create_command(cmd_name, isolated=("--isolated" in cmd_args))6869return command.main(cmd_args)707172