如何在 Ubuntu 上安装 pip

pip 是一个命令行工具,允许你安装 Python 编写的软件包。 学习如何在 Ubuntu 上安装 pip 以及如何使用它来安装 Python 应用程序。

有许多方法可以在 Ubuntu 上安装软件。 你可以从软件中心安装应用程序,也可以从下载的 DEB 文件、PPA(LCTT 译注:PPA 即 Personal Package Archives,个人软件包集)、Snap 软件包,也可以使用 Flatpak、使用 AppImage,甚至用旧的源代码安装方式。

还有一种方法可以在 Ubuntu 中安装软件包。 它被称为 pip,你可以使用它来安装基于 Python 的应用程序。

如何在 Ubuntu 上安装 pip

什么是 pip

pip 代表 “pip Installs Packages”。 pip 是一个基于命令行的包管理系统。 用于安装和管理 Python 语言编写的软件。

你可以使用 pip 来安装 Python 包索引(PyPI)中列出的包。

作为软件开发人员,你可以使用 pip 为你自己的 Python 项目安装各种 Python 模块和包。

作为最终用户,你可能需要使用 pip 来安装一些 Python 开发的并且可以使用 pip 轻松安装的应用程序。 一个这样的例子是 Stress Terminal 应用程序,你可以使用 pip 轻松安装。

让我们看看如何在 Ubuntu 和其他基于 Ubuntu 的发行版上安装 pip

如何在 Ubuntu 上安装 pip

默认情况下,pip 未安装在 Ubuntu 上。 你必须首先安装它才能使用。 在 Ubuntu 上安装 pip 非常简单。 我马上展示给你。

Ubuntu 18.04 默认安装了 Python 2 和 Python 3。 因此,你应该为两个 Python 版本安装 pip

pip,默认情况下是指 Python 2。pip3 代表 Python 3 中的 pip。

注意:我在本教程中使用的是 Ubuntu 18.04。 但是这里的教程应该适用于其他版本,如Ubuntu 16.04、18.10 等。你也可以在基于 Ubuntu 的其他 Linux 发行版上使用相同的命令,如 Linux Mint、Linux Lite、Xubuntu、Kubuntu 等。

为 Python 2 安装 pip

首先,确保已经安装了 Python 2。 在 Ubuntu 上,可以使用以下命令进行验证。

  1. <span class="pln">python2 </span><span class="pun">--</span><span class="pln">version</span>

如果没有错误并且显示了 Python 版本的有效输出,则说明安装了 Python 2。 所以现在你可以使用这个命令为 Python 2 安装 pip

  1. <span class="kwd">sudo</span><span class="pln"> apt install python</span><span class="pun">-</span><span class="pln">pip</span>

这将安装 pip 和它的许多其他依赖项。 安装完成后,请确认你已正确安装了 pip

  1. <span class="pln">pip </span><span class="pun">--</span><span class="pln">version</span>

它应该显示一个版本号,如下所示:

  1. <span class="pln">pip </span><span class="lit">9.0</span><span class="pun">.</span><span class="lit">1</span><span class="kwd">from</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">python2</span><span class="pun">.</span><span class="lit">7</span><span class="pun">/</span><span class="pln">dist</span><span class="pun">-</span><span class="pln">packages </span><span class="pun">(</span><span class="pln">python </span><span class="lit">2.7</span><span class="pun">)</span>

这意味着你已经成功在 Ubuntu 上安装了 pip

为 Python 3 安装 pip

你必须确保在 Ubuntu 上安装了 Python 3。 可以使用以下命令检查一下:

  1. <span class="pln">python3 </span><span class="pun">--</span><span class="pln">version</span>

如果显示了像 Python 3.6.6 这样的数字,则说明 Python 3 在你的 Linux 系统上安装好了。

现在,你可以使用以下命令安装 pip3

  1. <span class="kwd">sudo</span><span class="pln"> apt install python3</span><span class="pun">-</span><span class="pln">pip</span>

你应该使用以下命令验证 pip3 是否已正确安装:

  1. <span class="pln">pip3 </span><span class="pun">--</span><span class="pln">version</span>

它应该显示一个这样的数字:

  1. <span class="pln">pip </span><span class="lit">9.0</span><span class="pun">.</span><span class="lit">1</span><span class="kwd">from</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">python3</span><span class="pun">/</span><span class="pln">dist</span><span class="pun">-</span><span class="pln">packages </span><span class="pun">(</span><span class="pln">python </span><span class="lit">3.6</span><span class="pun">)</span>

这意味着 pip3 已成功安装在你的系统上。

如何使用 pip 命令

现在你已经安装了 pip,让我们快速看一些基本的 pip 命令。 这些命令将帮助你使用 pip 命令来搜索、安装和删除 Python 包。

要从 Python 包索引 PyPI 中搜索包,可以使用以下 pip 命令:

  1. <span class="pln">pip search </span><span class="pun"><</span><span class="pln">search_string</span><span class="pun">></span>

例如,如果你搜索“stress”这个词,将会显示名称或描述中包含字符串“stress”的所有包。

  1. <span class="pln">pip search stress</span>
  2. <span class="pln">stress </span><span class="pun">(</span><span class="lit">1.0</span><span class="pun">.</span><span class="lit">0</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A trivial utility </span><span class="kwd">for</span><span class="pln"> consuming system resources</span><span class="pun">.</span>
  3. <span class="pln">s</span><span class="pun">-</span><span class="pln">tui </span><span class="pun">(</span><span class="lit">0.8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Stress</span><span class="typ">Terminal</span><span class="pln"> UI stress </span><span class="kwd">test</span><span class="kwd">and</span><span class="pln"> monitoring tool</span>
  4. <span class="pln">stressypy </span><span class="pun">(</span><span class="lit">0.0</span><span class="pun">.</span><span class="lit">12</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A simple program </span><span class="kwd">for</span><span class="pln"> calling stress </span><span class="kwd">and</span><span class="pun">/</span><span class="kwd">or</span><span class="pln"> stress</span><span class="pun">-</span><span class="pln">ng </span><span class="kwd">from</span><span class="pln"> python</span>
  5. <span class="pln">fuzzing </span><span class="pun">(</span><span class="lit">0.3</span><span class="pun">.</span><span class="lit">2</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Tools</span><span class="kwd">for</span><span class="pln"> stress testing applications</span><span class="pun">.</span>
  6. <span class="pln">stressant </span><span class="pun">(</span><span class="lit">0.4</span><span class="pun">.</span><span class="lit">1</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Simple</span><span class="pln"> stress</span><span class="pun">-</span><span class="kwd">test</span><span class="pln"> tool</span>
  7. <span class="pln">stressberry </span><span class="pun">(</span><span class="lit">0.1</span><span class="pun">.</span><span class="lit">7</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Stress</span><span class="pln"> tests </span><span class="kwd">for</span><span class="pln"> the </span><span class="typ">Raspberry</span><span class="typ">Pi</span>
  8. <span class="pln">mobbage </span><span class="pun">(</span><span class="lit">0.2</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A HTTP stress </span><span class="kwd">test</span><span class="kwd">and</span><span class="pln"> benchmark tool</span>
  9. <span class="pln">stresser </span><span class="pun">(</span><span class="lit">0.2</span><span class="pun">.</span><span class="lit">1</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A large</span><span class="pun">-</span><span class="pln">scale stress testing framework</span><span class="pun">.</span>
  10. <span class="pln">cyanide </span><span class="pun">(</span><span class="lit">1.3</span><span class="pun">.</span><span class="lit">0</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Celery</span><span class="pln"> stress testing </span><span class="kwd">and</span><span class="pln"> integration </span><span class="kwd">test</span><span class="pln"> support</span><span class="pun">.</span>
  11. <span class="pln">pysle </span><span class="pun">(</span><span class="lit">1.5</span><span class="pun">.</span><span class="lit">7</span><span class="pun">)</span><span class="pun">-</span><span class="typ">An</span><span class="kwd">interface</span><span class="pln"> to ISLEX</span><span class="pun">,</span><span class="pln"> a pronunciation dictionary </span><span class="kwd">with</span><span class="pln"> stress markings</span><span class="pun">.</span>
  12. <span class="pln">ggf </span><span class="pun">(</span><span class="lit">0.3</span><span class="pun">.</span><span class="lit">2</span><span class="pun">)</span><span class="pun">-</span><span class="kwd">global</span><span class="pln"> geometric factors </span><span class="kwd">and</span><span class="pln"> corresponding stresses of the optical stretcher</span>
  13. <span class="pln">pathod </span><span class="pun">(</span><span class="lit">0.17</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A pathological HTTP</span><span class="pun">/</span><span class="pln">S daemon </span><span class="kwd">for</span><span class="pln"> testing </span><span class="kwd">and</span><span class="pln"> stressing clients</span><span class="pun">.</span>
  14. <span class="typ">MatPy</span><span class="pun">(</span><span class="lit">1.0</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A toolbox </span><span class="kwd">for</span><span class="pln"> intelligent material design</span><span class="pun">,</span><span class="kwd">and</span><span class="pln"> automatic </span><span class="kwd">yield</span><span class="pln"> stress determination</span>
  15. <span class="pln">netblow </span><span class="pun">(</span><span class="lit">0.1</span><span class="pun">.</span><span class="lit">2</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Vendor</span><span class="pln"> agnostic network testing framework to stress network failures</span>
  16. <span class="pln">russtress </span><span class="pun">(</span><span class="lit">0.1</span><span class="pun">.</span><span class="lit">3</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Package</span><span class="pln"> that helps you to put lexical stress </span><span class="kwd">in</span><span class="pln"> russian text</span>
  17. <span class="pln">switchy </span><span class="pun">(</span><span class="lit">0.1</span><span class="pun">.</span><span class="lit">0a1</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A fast </span><span class="typ">FreeSWITCH</span><span class="pln"> control library purpose</span><span class="pun">-</span><span class="pln">built on traffic theory </span><span class="kwd">and</span><span class="pln"> stress testing</span><span class="pun">.</span>
  18. <span class="pln">nx4_selenium_test </span><span class="pun">(</span><span class="lit">0.1</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Provides</span><span class="pln"> a </span><span class="typ">Python</span><span class="kwd">class</span><span class="kwd">and</span><span class="pln"> apps which monitor </span><span class="kwd">and</span><span class="pun">/</span><span class="kwd">or</span><span class="pln"> stress</span><span class="pun">-</span><span class="kwd">test</span><span class="pln"> the </span><span class="typ">NoMachine</span><span class="pln"> NX4 web </span><span class="kwd">interface</span>
  19. <span class="pln">physical_dualism </span><span class="pun">(</span><span class="lit">1.0</span><span class="pun">.</span><span class="lit">0</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Python</span><span class="pln"> library that approximates the natural frequency </span><span class="kwd">from</span><span class="pln"> stress via physical dualism</span><span class="pun">,</span><span class="kwd">and</span><span class="pln"> vice versa</span><span class="pun">.</span>
  20. <span class="pln">fsm_effective_stress </span><span class="pun">(</span><span class="lit">1.0</span><span class="pun">.</span><span class="lit">0</span><span class="pun">)</span><span class="pun">-</span><span class="typ">Python</span><span class="pln"> library that uses the rheological</span><span class="pun">-</span><span class="pln">dynamical analogy </span><span class="pun">(</span><span class="pln">RDA</span><span class="pun">)</span><span class="pln"> to compute damage </span><span class="kwd">and</span><span class="pln"> effective buckling stress </span><span class="kwd">in</span><span class="pln"> prismatic shell structures</span><span class="pun">.</span>
  21. <span class="pln">processpathway </span><span class="pun">(</span><span class="lit">0.3</span><span class="pun">.</span><span class="lit">11</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> A nifty little toolkit to create stress</span><span class="pun">-</span><span class="pln">free</span><span class="pun">,</span><span class="pln"> frustrationless image processing pathways </span><span class="kwd">from</span><span class="pln"> your webcam </span><span class="kwd">for</span><span class="pln"> computer vision experiments</span><span class="pun">.</span><span class="typ">Or</span><span class="pln"> observing your </span><span class="kwd">cat</span><span class="pun">.</span>

如果要使用 pip 安装应用程序,可以按以下方式使用它:

  1. <span class="pln">pip install </span><span class="pun"><</span><span class="pln">package_name</span><span class="pun">></span>

pip 不支持使用 tab 键补全包名,因此包名称需要准确指定。 它将下载所有必需的文件并安装该软件包。

如果要删除通过 pip 安装的 Python 包,可以使用 pip 中的 uninstall 选项。

  1. <span class="pln">pip uninstall </span><span class="pun"><</span><span class="pln">installed_package_name</span><span class="pun">></span>

你可以在上面的命令中使用 pip3 代替 pip

我希望这个快速提示可以帮助你在 Ubuntu 上安装 pip。 如果你有任何问题或建议,请在下面的评论部分告诉我。

via: https://itsfoss.com/install-pip-ubuntu/

作者:Abhishek Prakash 选题:lujun9972 译者:Flowsnow 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐