在本页

  • 先决条件
  • 为 Python3 安装 Pip
  • 为 Python2 安装 Pip
  • 如何使用 Pip 命令行
  • 结论

Pip 是 Python 编程语言中广泛使用的包管理器。它用于安装和管理 Python 标准库中不提供的其他软件包。它允许用户从 Python 包索引中搜索软件包以及安装其依赖项。Pip 也被称为“首选安装程序”(Preferred Installer Program),它可以为 Python 应用程序创建一个完全隔离的环境。

在本文中,我将向您展示如何在 Debian 11 上安装和使用 Pip。

先决条件

  • 一台运行 Debian 11 的服务器。
  • 服务器上已配置 root 密码。

为 Python3 安装 Pip

默认情况下,Debian 11 操作系统中未安装 Pip。您需要为 Python3 和 Python2 分别安装 Pip 版本。

首先,使用以下命令安装 Python3:

apt-get install python3 -y

安装 Python3 软件包后,使用以下命令为 Python3 安装 Pip:

apt-get install python3-pip -y

接下来,使用以下命令验证 Pip 版本:

pip3 --version

您将得到以下输出:

pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)

为 Python2 安装 Pip

首先,您需要在系统中安装 Python2。您可以使用以下命令安装它:

apt-get install python2 curl -y

接下来,使用以下命令下载 Pip2 安装脚本:

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

接下来,运行下载的脚本以在您的系统中安装 Pip2。

python2 get-pip.py

安装完成后,您可以使用以下命令验证 Pip2 版本:

pip2 --version

您将得到以下输出:

pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

如何使用 Pip 命令行

要列出 Pip 可用的所有选项,请运行以下命令:

pip3 --help

您将得到以下列表:

Usage:   
  pip3 
 [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 dependencies.
  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.

要为 Python3 安装任意软件包(如 TextStatistic),请运行以下命令:

pip3 install "TextStatistic"

示例输出:

Collecting TextStatistic
  Downloading TextStatistic-1.0.6-py3-none-any.whl (5.6 kB)
Installing collected packages: TextStatistic
Successfully installed TextStatistic-1.0.6

要为 Python2 安装任意软件包(如 scrapy),请运行以下命令:

pip install "scrapy"

要列出所有可用的软件包,请运行以下命令:

pip3 list

您应该看到以下输出:

Package          Version
---------------- ---------
certifi          2020.6.20
chardet          4.0.0
httplib2         0.18.1
idna             2.10
pip              20.3.4
pycurl           7.43.0.6
PySimpleSOAP     1.16.2
python-apt       2.2.1
python-debian    0.1.39
python-debianbts 3.1.0
reportbug        7.10.3
requests         2.25.1
setuptools       52.0.0
six              1.16.0
TextStatistic    1.0.6
urllib3          1.26.5
wheel            0.34.2

要搜索任意软件包,请运行以下命令:

pip3 search urllib3

要列出过时的软件包,请运行以下命令:

pip3 list --outdated

您将得到以下输出:

Package          Version   Latest    Type
---------------- --------- --------- -----
certifi          2020.6.20 2021.10.8 wheel
httplib2         0.18.1    0.20.1    wheel
idna             2.10      3.3       wheel
pip              20.3.4    21.3      wheel
pycurl           7.43.0.6  7.44.1    sdist
python-debian    0.1.39    0.1.40    wheel
python-debianbts 3.1.0     3.2.0     wheel
requests         2.25.1    2.26.0    wheel
setuptools       52.0.0    58.2.0    wheel
urllib3          1.26.5    1.26.7    wheel
wheel            0.34.2    0.37.0    wheel

要显示任意软件包的信息,请运行以下命令:

pip3 show wheel

您应该在以下输出中看到 wheel 软件包的信息:

Name: wheel
Version: 0.34.2
Summary: A built-package format for Python
Home-page: https://github.com/pypa/wheel
Author: Daniel Holth
Author-email: 
[email protected]

License: MIT
Location: /usr/lib/python3/dist-packages
Requires: 
Required-by: 

要卸载任意软件包,请运行以下命令:

pip3 uninstall scrapy

结论

在上面的指南中,我们解释了如何在 Debian 11 上安装 Pip3 和 Pip2。我们还解释了如何使用 Pip 命令来安装和管理 Python 软件包。希望您现在能够使用 Pip 命令轻松管理 Python 依赖项。