ensurepip
--- 引导 pip
安装器¶
3.4 新版功能.
ensurepip
包为在已有的Python安装实例或虚拟环境中引导 pip
安装器提供了支持。需要使用引导才能使用pip的这一事实也正好反映了 pip
是一个独立的项目,有其自己的发布周期,其最新版本随CPython解释器的维护版本和新特性版本一同捆绑。
在大多数情况下,Python的终端使用者不需要直接调用这个模块( pip
默认应该已被引导),不过,如果在安装Python(或创建虚拟环境)之时跳过了安装 pip
步骤,或者日后特意卸载了 pip
,则需要使用这个模块。
注解
这个模块 无需 访问互联网。引导启动 pip
所需的全部组件均包含在包的内部。
参见
- 安装 Python 模块
安装Python包的终端使用者教程
- PEP 453: 在Python安装实例中显式引导启动pip
这个模块的原始缘由以及规范文档
命令行界面¶
使用解释器的 -m
参数调用命令行接口。
最简单的调用方式为:
python -m ensurepip
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:
python -m ensurepip --upgrade
在默认情况下,pip
会被安装到当前虚拟环境(如果激活了虚拟环境)或系统的包目录(如果未激活虚拟环境)。 安装位置可通过两个额外的命令行选项来控制。
--root 1
: 相对于给定的根目录而不是当前已激活虚拟环境(如果存在)的根目录或当前 Python 安装版的默认根目录来安装pip
。--user
: 将pip
安装到用户包目录而不是全局安装到当前 Python 安装版(此选项不允许在已激活虚拟环境中使用)。
在默认情况下,脚本 pipX
和 pipX.Y
将被安装(其中 X.Y 表示被用来发起调用 ensurepip
的 Python 的版本)。 所安装的脚本可通过两个额外的命令行选项来控制:
--altinstall
: 如果请求了一个替代安装版,则pipX
脚本将 不会 被安装。--default-pip
: 如果请求了一个 "默认的 pip" 安装版,则除了两个常规脚本之外还将安装pip
脚本。
同时提供这两个脚本选择选项将会触发异常。
模块 API¶
ensurepip
暴露了两个函数用于编程:
-
ensurepip.
version
()¶ 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)¶ 初始创建
pip
到当前的或指定的环境中。root 指明要作为相对安装路径的替代根目录。 如果 root 为
None
,则安装会使用当前环境的默认安装位置。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
andpipX.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
sys.stdout
from the bootstrapping operation.Raises an auditing event
ensurepip.bootstrap
with argumentroot
.注解
The bootstrapping process has side effects on both
sys.path
andos.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 ofpip
).