Path: blob/master/venv/Lib/site-packages/pip/_internal/cli/main.py
811 views
"""Primary application entrypoint.1"""2from __future__ import absolute_import34import locale5import logging6import os7import sys89from pip._internal.cli.autocompletion import autocomplete10from pip._internal.cli.main_parser import parse_command11from pip._internal.commands import create_command12from pip._internal.exceptions import PipError13from pip._internal.utils import deprecation14from pip._internal.utils.typing import MYPY_CHECK_RUNNING1516if MYPY_CHECK_RUNNING:17from typing import List, Optional1819logger = logging.getLogger(__name__)202122# Do not import and use main() directly! Using it directly is actively23# discouraged by pip's maintainers. The name, location and behavior of24# this function is subject to change, so calling it directly is not25# portable across different pip versions.2627# In addition, running pip in-process is unsupported and unsafe. This is28# elaborated in detail at29# https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program.30# That document also provides suggestions that should work for nearly31# all users that are considering importing and using main() directly.3233# However, we know that certain users will still want to invoke pip34# in-process. If you understand and accept the implications of using pip35# in an unsupported manner, the best approach is to use runpy to avoid36# depending on the exact location of this entry point.3738# The following example shows how to use runpy to invoke pip in that39# case:40#41# sys.argv = ["pip", your, args, here]42# runpy.run_module("pip", run_name="__main__")43#44# Note that this will exit the process after running, unlike a direct45# call to main. As it is not safe to do any processing after calling46# main, this should not be an issue in practice.4748def main(args=None):49# type: (Optional[List[str]]) -> int50if args is None:51args = sys.argv[1:]5253# Configure our deprecation warnings to be sent through loggers54deprecation.install_warning_logger()5556autocomplete()5758try:59cmd_name, cmd_args = parse_command(args)60except PipError as exc:61sys.stderr.write("ERROR: {}".format(exc))62sys.stderr.write(os.linesep)63sys.exit(1)6465# Needed for locale.getpreferredencoding(False) to work66# in pip._internal.utils.encoding.auto_decode67try:68locale.setlocale(locale.LC_ALL, '')69except locale.Error as e:70# setlocale can apparently crash if locale are uninitialized71logger.debug("Ignoring error %s when setting locale", e)72command = create_command(cmd_name, isolated=("--isolated" in cmd_args))7374return command.main(cmd_args)757677