ensurepipBootstrapping the pip installer引导pip安装程序

New 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 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.在大多数情况下,Python的最终用户不需要直接调用此模块(因为默认情况下应该引导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). 默认情况下,将安装脚本pipXpipX.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 None, then installation uses the default install location for the current environment.如果rootNone,则安装将使用当前环境的默认安装位置。

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 and pipX.Y will be installed (where X.Y stands for the current version of Python).默认情况下,将安装脚本pipXpipX.Y(其中X.Y代表Python的当前版本)。

If altinstall is set, then pipX will not be installed.如果设置了altinstall,则不会安装pipX

If default_pip is set, then pip will be installed in addition to the two regular scripts.如果设置了default_pip,那么除了两个常规脚本之外,还会安装pip。

Setting both altinstall and default_pip will trigger ValueError.同时设置altinstalldefault_pip将触ValueError

verbosity controls the level of output to sys.stdout from the bootstrapping operation.verbosity控制从引导操作到sys.stdout的输出级别。

Raises an auditing event ensurepip.bootstrap with argument root.引发一个带有参数root审核事件ensurepip.bootstrap

Note

The bootstrapping process has side effects on both sys.path and os.environ. 自举过程对sys.pathos.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 of pip).自举过程可能会安装pip所需的额外模块,但其他软件不应假设这些依赖关系在默认情况下总是存在(因为这些依赖关系可能会在未来版本的pip中删除)。