Python 包安装程序 PIP

2023-09-17 22:16:43

PIP是 Python 的包安装程序。它用于从Python Package Index(PyPI)和其他索引安装软件包。

PyPI - Python 包索引(PyPI - Python Package Index)

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

安装点(Install pip)

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

C:>pip --version
pip 18.0 from c:python37libsite-packagespip (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

此命令将在您的 PC 中安装 pip。此外,它还安装了 wheelsetuptools .

画中画帮助命令(pip Help command)

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:passwd@]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.

安装软件包(Installing packages)

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

Pip install "project-name"

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

pip install "project-name==2.4"

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

pip install "project-name~=2.4"

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

C:MyProject> Pip install urllib3

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

t

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

列出包(List packages)

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包也列在那里。

显示套餐(Show package)

如果要检查包的元数据,请使用 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]
License: MIT
Location: c:python37libsite-packages
Requires:
Required-by: requests
卸载

软件包(Uninstalling a Package)

pip uninstall命令可用于删除包。例如,如果要删除urllib3包,只需使用以下命令:

$ pip uninstall urllib3

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

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

本文内容总结: