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 应用的包管理器。*

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/278307.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-19 13:22
下一篇 2024-12-19 13:22

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29

发表回复

登录后才能评论