ensurepip
— Bootstrapping the pip
installer引导pip
安装程序¶
pip
installerNew in version 3.4.版本3.4中新增。
The ensurepip
package provides support for bootstrapping the pip
installer into an existing Python installation or virtual environment. ensurepip
包支持将pip
安装程序引导到现有的Python安装或虚拟环境中。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.pip
是一个独立的项目,有自己的发布周期,最新可用的稳定版本与CPython参考解释器的维护和功能发布捆绑在一起。
In most cases, end users of Python shouldn’t need to invoke this module directly (as 在大多数情况下,Python的最终用户不需要直接调用此模块(因为默认情况下应该引导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
.pip
),但如果在安装Python(或创建虚拟环境时)或显式卸载pip后跳过了安装pip
,则可能需要调用此模块。
Note
This module does not access the internet. 此模块无法访问互联网。All of the components needed to bootstrap 引导pip
are included as internal parts of the package.pip
所需的所有组件都作为包的内部部件包含在内。
See also
Installing Python Modules安装Python模块The end user guide for installing Python packages安装Python包的最终用户指南- PEP 453
: Explicit bootstrapping of pip in Python installations:在Python安装中显式引导pip The original rationale and specification for this module.该模块的原始原理和规范。
Command line interface命令行接口¶
The command line interface is invoked using the interpreter’s 命令行接口是使用解释器的-m
switch.-m
开关调用的。
The simplest possible invocation is:最简单的调用是:
python -m ensurepip
This invocation will install 如果尚未安装pip
if it is not already installed, but otherwise does nothing. pip
,则此调用将安装它,但在其他情况下不执行任何操作。To ensure the installed version of 要确保安装的pip
is at least as recent as the one available in ensurepip
, pass the --upgrade
option:pip
版本至少与ensurepip
中可用的版本一样新,请传递--upgrade
选项:
python -m ensurepip --upgrade
By default, 默认情况下,pip
is installed into the current virtual environment (if one is active) or into the system site packages (if there is no active virtual environment). pip
安装到当前虚拟环境中(如果其中一个处于活动状态)或系统站点包中(如果没有活动虚拟环境)。The installation location can be controlled through two additional command line options:可以通过两个额外的命令行选项控制安装位置:
--root <dir>
: Installs:相对于给定的根目录安装pip
relative to the given root directory rather than the root of the currently active virtual environment (if any) or the default root for the current Python installation.pip
,而不是当前活动虚拟环境的根目录(如果有)或当前Python安装的默认根目录。--user
: Installs:将pip
into the user site packages directory rather than globally for the current Python installation (this option is not permitted inside an active virtual environment).pip
安装到用户站点软件包目录中,而不是全局安装当前Python(活动虚拟环境中不允许使用此选项)。
By default, the scripts 默认情况下,将安装脚本pipX
and pipX.Y
will be installed (where X.Y stands for the version of Python used to invoke ensurepip
). pipX
和pipX.Y
(其中X.Y
代表用于调用ensurepip套间的Python版本)。The scripts installed can be controlled through two additional command line options:安装的脚本可以通过两个额外的命令行选项进行控制:
--altinstall
: if an alternate installation is requested, the:如果请求备用安装,则不会安装pipX
script will not be installed.pipX
脚本。--default-pip
: if a “default pip” installation is requested, the:如果请求“默认pip
script will be installed in addition to the two regular scripts.pip
”安装,除了两个常规脚本外,还会安装pip脚本。
Providing both of the script selection options will trigger an exception.提供这两个脚本选择选项将触发一个异常。
Module API¶
ensurepip
exposes two functions for programmatic use:公开两个函数以供编程使用:
-
ensurepip.
version
()¶ Returns a string specifying the available version of pip that will be installed when bootstrapping an environment.返回一个字符串,指定在引导环境时将安装的pip的可用版本。
-
ensurepip.
bootstrap
(root=None, upgrade=False, user=False, altinstall=False, default_pip=False, verbosity=0)¶ Bootstraps引导pip
into the current or designated environment.pip
进入当前或指定的环境。root
specifies an alternative root directory to install relative to.指定要安装的替代根目录。If root is如果root为None
, then installation uses the default install location for the current environment.None
,则安装将使用当前环境的默认安装位置。upgrade
indicates whether or not to upgrade an existing installation of an earlier version of指示是否将早期版本的pip
to the available version.pip
的现有安装升级到可用版本。user
indicates whether to use the user scheme rather than installing globally.指示是否使用用户方案而不是全局安装。By default, the scripts默认情况下,将安装脚本pipX
andpipX.Y
will be installed (where X.Y stands for the current version of Python).pipX
和pipX.Y
(其中X.Y
代表Python的当前版本)。If altinstall is set, then如果设置了altinstall,则不会安装pipX
will not be installed.pipX
。If default_pip is set, then如果设置了default_pip,那么除了两个常规脚本之外,还会安装pip。pip
will be installed in addition to the two regular scripts.Setting both altinstall and default_pip will trigger同时设置altinstall和default_pip将触ValueError
.ValueError
。verbosity controls the level of output toverbosity控制从引导操作到sys.stdout
from the bootstrapping operation.sys.stdout
的输出级别。Raises an auditing event引发一个带有参数ensurepip.bootstrap
with argumentroot
.root
的审核事件ensurepip.bootstrap
。Note
The bootstrapping process has side effects on both自举过程对sys.path
andos.environ
.sys.path
和os.environ
都有副作用。Invoking the command line interface in a subprocess instead allows these side effects to be avoided.相反,在子流程中调用命令行接口可以避免这些副作用。Note
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 ofpip
).pip
所需的额外模块,但其他软件不应假设这些依赖关系在默认情况下总是存在(因为这些依赖关系可能会在未来版本的pip中删除)。