28.2. ensurepip — Bootstrapping the pip
installer
The package provides support for bootstrapping the pip
installer into an existing Python installation or virtual environment. This bootstrapping approach reflects the fact that pip
is an independent project with its own release cycle, and the latest available stable version is bundled with maintenance and feature releases of the CPython reference interpreter.
In most cases, end users of Python shouldn’t need to invoke this module directly (as pip
should be bootstrapped by default), but it may be needed if installing pip
was skipped when installing Python (or when creating a virtual environment) or after explicitly uninstalling pip
.
注解
This module does not access the internet. All of the components needed to bootstrap pip
are included as internal parts of the package.
参见
The end user guide for installing Python packages
: Explicit bootstrapping of pip in Python installations
The original rationale and specification for this module.
The command line interface is invoked using the interpreter’s -m
switch.
The simplest possible invocation is:
This invocation will install pip
if it is not already installed, but otherwise does nothing. To ensure the installed version of pip
is at least as recent as the one bundled with ensurepip
, pass the --upgrade
option:
--user
: Installspip
into the user site packages directory rather than globally for the current Python installation (this option is not permitted inside an active virtual environment).
By default, the scripts pipX
and pipX.Y
will be installed (where X.Y stands for the version of Python used to invoke ensurepip
). The scripts installed can be controlled through two additional command line options:
--default-pip
: if a “default pip” installation is requested, thepip
script will be installed in addition to the two regular scripts.
Providing both of the script selection options will trigger an exception.
在 3.6.3 版更改: The exit status is non-zero if the command fails.
28.2.2. Module API
exposes two functions for programmatic use:
()
Returns a string specifying the bundled version of pip that will be installed when bootstrapping an environment.
ensurepip.bootstrap
(root=None, upgrade=False, user=False, altinstall=False, default_pip=False, verbosity=0)
root specifies an alternative root directory to install relative to. If root is None
, then installation uses the default install location for the current environment.
upgrade indicates whether or not to upgrade an existing installation of an earlier version of pip
to the bundled version.
user indicates whether to use the user scheme rather than installing globally.
By default, the scripts pipX
and pipX.Y
will be installed (where X.Y stands for the current version of Python).
If altinstall is set, then pipX
will not be installed.
If default_pip is set, then pip
will be installed in addition to the two regular scripts.
Setting both altinstall and default_pip will trigger ValueError.
verbosity controls the level of output to from the bootstrapping operation.
注解
The bootstrapping process has side effects on both sys.path
and os.environ
. Invoking the command line interface in a subprocess instead allows these side effects to be avoided.
注解
The bootstrapping process may install additional modules required by pip
, but other software should not assume those dependencies will always be present by default (as the dependencies may be removed in a future version of pip
).