您的位置:

Python 的 PIP 包安装程序

PIP 是 Python 的包安装程序。用于安装来自 Python 包索引 (PyPI)等索引的包。

Python 包索引

PyPI 是 Python 社区的 Python 包的默认存储库,包括框架、工具和库。Python 开发人员可以在 Python 应用中安装和使用包。所有 Python 开发人员都可以使用和分发他们的发行版。 开发者可以搜索或浏览来自 pypi.org 的项目。

安装 pip

从 Python 3.4 开始,Python 安装程序中就包含了 PIP。您可以通过在控制台中运行以下命令来验证 pip 是否安装在您的计算机上:

C:\>pip --version
pip 18.0 from c:\python37\lib\site-packages\pip (python 3.7)

如果您使用的是旧版本的 pip,可以通过在 Windows 上运行以下命令来升级 pip:

C:\> python -m pip install -U pip

在 Linux 或 Mac OS 上执行以下命令来升级 pip:

$ pip install -U pip

如果 pip 尚未安装,则首先通过在控制台或终端窗口中执行以下命令,尝试从标准库中引导它:

python -m ensurepip --default-pip

如果仍然没有安装 pip,下面的步骤应该会在您的平台上安装 pip。

  1. 从https://bootstrap.pypa.io/get-pip.py下载get-pip.py保存到本地文件夹。
  2. 导航命令提示符或终端到您下载文件的文件夹,运行命令:python get-pip.py

这个命令将在你的电脑上安装 PIP。此外,它还安装了轮和设置工具。

pip 帮助命令

pip help命令用于获取 pip 中所有可用功能的列表。

C:\> pip help

Usage:
pip <command> [options]

Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependen cies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.

General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring
environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be
used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be
used up to 3 times (corresponding to WARNING,
ERROR, and CRITICAL logging levels).
--log <path > Path to a verbose appending log.
--no-input Disable prompting for input.
--proxy <proxy > Specify a proxy in the form [user:[[email protected]](/cdn-cgi/l/email-protection)]proxy.server:port.
--retries <retries > Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec > Set the socket timeout (default 15 seconds).
--exists-action <action > Default action when a path already exists:
(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname > Mark this host or host:port pair as trusted,
even though it does not have valid or any HTTPS.
--cert <path > Path to alternate CA bundle.
--client-cert <path > Path to SSL client certificate, a single file
containing the private key and the certificate
in PEM format.
--cache-dir <dir > Store the cache data in <dir >.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine
whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output
--no-python-version-warning
Silence deprecation warnings for upcoming
unsupported Pythons.
--use-feature <feature > Enable new functionality, that may be backward
incompatible.
--use-deprecated <feature > Enable deprecated functionality, that will be
removed in the future.

安装包

PyPI 将包维护为项目。使用以下 pip 命令安装项目的最新版本。

Pip install "project-name"

使用以下命令安装项目的特定版本:

pip install "project-name==2.4"

使用以下命令安装与特定版本“兼容”的版本:

pip install "project-name~=2.4"

让我们安装一个包,用 Python 发送一个 HTTP 请求。urllib3 是一个功能强大、用户友好的 Python HTTP 客户端。在应用中使用 urllib3 包之前,请使用pip命令安装它,如下所示。

C:\MyProject> Pip install urllib3

以上命令将安装最新版本的urllib3。现在,您可以导入并使用它,如下所示。

Example: Use urllib3 Package

import urllib3

http = urllib3.PoolManager()
req = http.request('GET', 'http://www.google.com')
print(req.status) 

列出包

list 命令可用于查看系统上已安装的所有包。如果您想检查所有包装,使用pip list命令:

C:\> pip list

Package Version
------- -------
argon2-cffi 20.1.0
async-generator 1.10
attrs 20.2.0
backcall 0.1.0
bleach 3.2.1
certifi 2020.6.20
cffi 1.14.3
chardet 3.0.4
colorama 0.3.9
decorator 4.3.0
defusedxml 0.6.0
entrypoints 0.3
idna 2.10
importlib-metadata 2.0.0
ipykernel 5.3.4
ipython 6.5.0
ipython-genutils 0.2.0
jedi 0.12.1
Jinja2 2.11.2
json5 0.9.5
jsonschema 3.2.0
jupyter-client 6.1.7
jupyter-core 4.6.3
jupyterlab-pygments 0.1.2
jupyterlab-server 1.2.0
MarkupSafe 1.1.1
mistune 0.8.4
mongobox 0.1.8
nbclient 0.5.0
nbconvert 6.0.7
nbformat 5.0.7
nest-asyncio 1.4.1
notebook 6.1.4
packaging 20.4
pandocfilters 1.4.2
parso 0.3.1
pickleshare 0.7.4
pip 20.2.4
prometheus-client 0.8.0
prompt-toolkit 1.0.15
pycparser 2.20
Pygments 2.7.1
pyparsing 2.4.7
pyrsistent 0.17.3
python-dateutil 2.8.1
pywin32 228
pywinpty 0.5.7
pyzmq 19.0.2
requests 2.24.0
Send2Trash 1.5.0
setuptools 39.0.1
simplegeneric 0.8.1
six 1.11.0
terminado 0.9.1
testpath 0.4.4
tornado 6.0.4
traitlets 4.3.2
urllib3 1.26.2
wcwidth 0.1.7
webencodings 0.5.1
zipp 3.3.0

这将列出系统中可用的所有包。注意urllib3包也列在那里。

显示包

如果要检查包的元数据,请使用 pip show 命令。以下命令将显示 urllib3 包的元数据。

C:\> pip show urllib3

Name: urllib3
Version: 1.26.2
Summary: HTTP library with thread-safe connection pooling, file post, and more.
Home-page: https://urllib3.readthedocs.io
Author: Andrey Petrov
Author-email: [[email protected]](/cdn-cgi/l/email-protection)
License: MIT
Location: c:\python37\lib\site-packages
Requires:
Required-by: requests

卸载包

pip uninstall命令可用于移除包裹。例如,如果您想删除urllib3包,您可以简单地使用以下命令:

$ pip uninstall urllib3

pip 包管理器将要求您确认是否要卸载该包。Proceed (y/n)?: 如果按 y,包裹将被移除。

因此,您可以使用 pip 作为 Python 应用的包管理器。*