17db96d56Sopenharmony_ci:mod:`ensurepip` --- Bootstrapping the ``pip`` installer
27db96d56Sopenharmony_ci========================================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: ensurepip
57db96d56Sopenharmony_ci   :synopsis: Bootstrapping the "pip" installer into an existing Python
67db96d56Sopenharmony_ci              installation or virtual environment.
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci.. versionadded:: 3.4
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci**Source code:** :source:`Lib/ensurepip`
117db96d56Sopenharmony_ci
127db96d56Sopenharmony_ci--------------
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ciThe :mod:`ensurepip` package provides support for bootstrapping the ``pip``
157db96d56Sopenharmony_ciinstaller into an existing Python installation or virtual environment. This
167db96d56Sopenharmony_cibootstrapping approach reflects the fact that ``pip`` is an independent
177db96d56Sopenharmony_ciproject with its own release cycle, and the latest available stable version
187db96d56Sopenharmony_ciis bundled with maintenance and feature releases of the CPython reference
197db96d56Sopenharmony_ciinterpreter.
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ciIn most cases, end users of Python shouldn't need to invoke this module
227db96d56Sopenharmony_cidirectly (as ``pip`` should be bootstrapped by default), but it may be
237db96d56Sopenharmony_cineeded if installing ``pip`` was skipped when installing Python (or
247db96d56Sopenharmony_ciwhen creating a virtual environment) or after explicitly uninstalling
257db96d56Sopenharmony_ci``pip``.
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ci.. note::
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci   This module *does not* access the internet. All of the components
307db96d56Sopenharmony_ci   needed to bootstrap ``pip`` are included as internal parts of the
317db96d56Sopenharmony_ci   package.
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci.. seealso::
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci   :ref:`installing-index`
367db96d56Sopenharmony_ci      The end user guide for installing Python packages
377db96d56Sopenharmony_ci
387db96d56Sopenharmony_ci   :pep:`453`: Explicit bootstrapping of pip in Python installations
397db96d56Sopenharmony_ci      The original rationale and specification for this module.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ci.. include:: ../includes/wasm-notavail.rst
427db96d56Sopenharmony_ci
437db96d56Sopenharmony_ciCommand line interface
447db96d56Sopenharmony_ci----------------------
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ciThe command line interface is invoked using the interpreter's ``-m`` switch.
477db96d56Sopenharmony_ci
487db96d56Sopenharmony_ciThe simplest possible invocation is::
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ci    python -m ensurepip
517db96d56Sopenharmony_ci
527db96d56Sopenharmony_ciThis invocation will install ``pip`` if it is not already installed,
537db96d56Sopenharmony_cibut otherwise does nothing. To ensure the installed version of ``pip``
547db96d56Sopenharmony_ciis at least as recent as the one available in ``ensurepip``, pass the
557db96d56Sopenharmony_ci``--upgrade`` option::
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ci    python -m ensurepip --upgrade
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ciBy default, ``pip`` is installed into the current virtual environment
607db96d56Sopenharmony_ci(if one is active) or into the system site packages (if there is no
617db96d56Sopenharmony_ciactive virtual environment). The installation location can be controlled
627db96d56Sopenharmony_cithrough two additional command line options:
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci* ``--root <dir>``: Installs ``pip`` relative to the given root directory
657db96d56Sopenharmony_ci  rather than the root of the currently active virtual environment (if any)
667db96d56Sopenharmony_ci  or the default root for the current Python installation.
677db96d56Sopenharmony_ci* ``--user``: Installs ``pip`` into the user site packages directory rather
687db96d56Sopenharmony_ci  than globally for the current Python installation (this option is not
697db96d56Sopenharmony_ci  permitted inside an active virtual environment).
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ciBy default, the scripts ``pipX`` and ``pipX.Y`` will be installed (where
727db96d56Sopenharmony_ciX.Y stands for the version of Python used to invoke ``ensurepip``). The
737db96d56Sopenharmony_ciscripts installed can be controlled through two additional command line
747db96d56Sopenharmony_cioptions:
757db96d56Sopenharmony_ci
767db96d56Sopenharmony_ci* ``--altinstall``: if an alternate installation is requested, the ``pipX``
777db96d56Sopenharmony_ci  script will *not* be installed.
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ci* ``--default-pip``: if a "default pip" installation is requested, the
807db96d56Sopenharmony_ci  ``pip`` script will be installed in addition to the two regular scripts.
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ciProviding both of the script selection options will trigger an exception.
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci
857db96d56Sopenharmony_ciModule API
867db96d56Sopenharmony_ci----------
877db96d56Sopenharmony_ci
887db96d56Sopenharmony_ci:mod:`ensurepip` exposes two functions for programmatic use:
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ci.. function:: version()
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci   Returns a string specifying the available version of pip that will be
937db96d56Sopenharmony_ci   installed when bootstrapping an environment.
947db96d56Sopenharmony_ci
957db96d56Sopenharmony_ci.. function:: bootstrap(root=None, upgrade=False, user=False, \
967db96d56Sopenharmony_ci                        altinstall=False, default_pip=False, \
977db96d56Sopenharmony_ci                        verbosity=0)
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ci   Bootstraps ``pip`` into the current or designated environment.
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ci   *root* specifies an alternative root directory to install relative to.
1027db96d56Sopenharmony_ci   If *root* is ``None``, then installation uses the default install location
1037db96d56Sopenharmony_ci   for the current environment.
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ci   *upgrade* indicates whether or not to upgrade an existing installation
1067db96d56Sopenharmony_ci   of an earlier version of ``pip`` to the available version.
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ci   *user* indicates whether to use the user scheme rather than installing
1097db96d56Sopenharmony_ci   globally.
1107db96d56Sopenharmony_ci
1117db96d56Sopenharmony_ci   By default, the scripts ``pipX`` and ``pipX.Y`` will be installed (where
1127db96d56Sopenharmony_ci   X.Y stands for the current version of Python).
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ci   If *altinstall* is set, then ``pipX`` will *not* be installed.
1157db96d56Sopenharmony_ci
1167db96d56Sopenharmony_ci   If *default_pip* is set, then ``pip`` will be installed in addition to
1177db96d56Sopenharmony_ci   the two regular scripts.
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ci   Setting both *altinstall* and *default_pip* will trigger
1207db96d56Sopenharmony_ci   :exc:`ValueError`.
1217db96d56Sopenharmony_ci
1227db96d56Sopenharmony_ci   *verbosity* controls the level of output to :data:`sys.stdout` from the
1237db96d56Sopenharmony_ci   bootstrapping operation.
1247db96d56Sopenharmony_ci
1257db96d56Sopenharmony_ci   .. audit-event:: ensurepip.bootstrap root ensurepip.bootstrap
1267db96d56Sopenharmony_ci
1277db96d56Sopenharmony_ci   .. note::
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ci      The bootstrapping process has side effects on both ``sys.path`` and
1307db96d56Sopenharmony_ci      ``os.environ``. Invoking the command line interface in a subprocess
1317db96d56Sopenharmony_ci      instead allows these side effects to be avoided.
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci   .. note::
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ci      The bootstrapping process may install additional modules required by
1367db96d56Sopenharmony_ci      ``pip``, but other software should not assume those dependencies will
1377db96d56Sopenharmony_ci      always be present by default (as the dependencies may be removed in a
1387db96d56Sopenharmony_ci      future version of ``pip``).
139