合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](venv.xhtml "venv --- 创建虚拟环境") | - [上一页](distutils.xhtml "distutils --- 构建和安装 Python 模块") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [软件打包和分发](distribution.xhtml) » - $('.inline-search').show(0); | # [`ensurepip`](#module-ensurepip "ensurepip: Bootstrapping the "pip" installer into an existing Python installation or virtual environment.") --- Bootstrapping the `pip` installer 3\.4 新版功能. - - - - - - The [`ensurepip`](#module-ensurepip "ensurepip: Bootstrapping the "pip" installer into an existing Python installation or virtual environment.") 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. 参见 [安装 Python 模块](../installing/index.xhtml#installing-index)The end user guide for installing Python packages [**PEP 453**](https://www.python.org/dev/peps/pep-0453) \[https://www.python.org/dev/peps/pep-0453\]: Explicit bootstrapping of pip in Python installationsThe original rationale and specification for this module. ## Command line interface The command line interface is invoked using the interpreter's `-m` switch. The simplest possible invocation is: ``` 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 ``` 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). 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. - `--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). 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: - `--altinstall`: if an alternate installation is requested, the `pipX`script will *not* be installed. - `--default-pip`: if a "default pip" installation is requested, the`pip` script will be installed in addition to the two regular scripts. Providing both of the script selection options will trigger an exception. ## Module API [`ensurepip`](#module-ensurepip "ensurepip: Bootstrapping the "pip" installer into an existing Python installation or virtual environment.") exposes two functions for programmatic use: `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*)Bootstraps `pip` into the current or designated environment. *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`](exceptions.xhtml#ValueError "ValueError"). *verbosity* controls the level of output to [`sys.stdout`](sys.xhtml#sys.stdout "sys.stdout") 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`). ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](venv.xhtml "venv --- 创建虚拟环境") | - [上一页](distutils.xhtml "distutils --- 构建和安装 Python 模块") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [软件打包和分发](distribution.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。