main.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Primary application entrypoint.
  2. """
  3. import locale
  4. import logging
  5. import os
  6. import sys
  7. import warnings
  8. from typing import List, Optional
  9. from pip._internal.cli.autocompletion import autocomplete
  10. from pip._internal.cli.main_parser import parse_command
  11. from pip._internal.commands import create_command
  12. from pip._internal.exceptions import PipError
  13. from pip._internal.utils import deprecation
  14. logger = logging.getLogger(__name__)
  15. # Do not import and use main() directly! Using it directly is actively
  16. # discouraged by pip's maintainers. The name, location and behavior of
  17. # this function is subject to change, so calling it directly is not
  18. # portable across different pip versions.
  19. # In addition, running pip in-process is unsupported and unsafe. This is
  20. # elaborated in detail at
  21. # https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program.
  22. # That document also provides suggestions that should work for nearly
  23. # all users that are considering importing and using main() directly.
  24. # However, we know that certain users will still want to invoke pip
  25. # in-process. If you understand and accept the implications of using pip
  26. # in an unsupported manner, the best approach is to use runpy to avoid
  27. # depending on the exact location of this entry point.
  28. # The following example shows how to use runpy to invoke pip in that
  29. # case:
  30. #
  31. # sys.argv = ["pip", your, args, here]
  32. # runpy.run_module("pip", run_name="__main__")
  33. #
  34. # Note that this will exit the process after running, unlike a direct
  35. # call to main. As it is not safe to do any processing after calling
  36. # main, this should not be an issue in practice.
  37. def main(args: Optional[List[str]] = None) -> int:
  38. if args is None:
  39. args = sys.argv[1:]
  40. # Suppress the pkg_resources deprecation warning
  41. # Note - we use a module of .*pkg_resources to cover
  42. # the normal case (pip._vendor.pkg_resources) and the
  43. # devendored case (a bare pkg_resources)
  44. warnings.filterwarnings(
  45. action="ignore", category=DeprecationWarning, module=".*pkg_resources"
  46. )
  47. # Configure our deprecation warnings to be sent through loggers
  48. deprecation.install_warning_logger()
  49. autocomplete()
  50. try:
  51. cmd_name, cmd_args = parse_command(args)
  52. except PipError as exc:
  53. sys.stderr.write(f"ERROR: {exc}")
  54. sys.stderr.write(os.linesep)
  55. sys.exit(1)
  56. # Needed for locale.getpreferredencoding(False) to work
  57. # in pip._internal.utils.encoding.auto_decode
  58. try:
  59. locale.setlocale(locale.LC_ALL, "")
  60. except locale.Error as e:
  61. # setlocale can apparently crash if locale are uninitialized
  62. logger.debug("Ignoring error %s when setting locale", e)
  63. command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  64. return command.main(cmd_args)