如何在Ubuntu上安装pip

时间:2020-03-05 15:32:39  来源:igfitidea点击:

pip是一个包管理器,它促进了Python软件包的安装和管理,例如,Python Package索引(PYPI)中包含的软件包。

在本文中,将介绍如何在Ubuntu 18.04上安装和使用pip。

在Ubuntu上安装Python3的pip

默认情况下,Ubuntu 18.04在其安装中附带Python3.

要为Python3安装PIP3,请按以下步骤操作

更新系统

# sudo apt-get update

为Python3安装pip

# sudo apt-get install python3-pip

此命令将pip与构建Python模块所需的所有依赖项一起安装。

示例输出

The following NEW packages will be installed:
libpython3-dev libpython3.4 libpython3.4-dev python3-chardet
python3-colorama python3-dev python3-distlib python3-html5lib python3-pip
python3-requests python3-setuptools python3-six python3-urllib3
python3-wheel python3.4-dev
0 upgraded, 15 newly installed, 0 to remove and 26 not upgraded.
Need to get 20.2 MB of archives.
After this operation, 38.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

检查PIP3的版本

要检查PIP的版本,请运行以下命令

# pip3 --version

或者

# pip3 -V

输出

pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

在Ubuntu上安装Python2的pip

默认情况下,Python2未在Ubuntu 18.04上安装。
如果要为Python 2安装Python2和PIP,请运行以下命令

更新系统

# sudo apt-get update

为python2安装pip

# sudo apt-get install python-pip

检查PIP版本

验证PIP的安装,运行

# pip --version

或者

# pip -V

输出

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

在包管理中使用pip

现在我们已经看到了如何在不同的Python环境中安装各种版本的PIP,是时候了解如何使用它来安装,升级和卸载软件包。

使用pip安装包

安装Python模块时,建议我们在虚拟环境中执行此操作。
虚拟环境为几个Python项目创建孤立的环境。
这允许我们每个项目安装特定模块,而不担心影响其他Python项目的模块。

使用pip安装包装

# pip3 install package_name

例如

# pip install numpy

输出

Collecting numpy
  Downloading https://files.pythonhosted.org/packages/40/c5/f1ed15dd931d6667b40f                                                                                        1ab1c2fe1f26805fc2b6c3e25e45664f838de9d0/numpy-1.15.2-cp27-cp27mu-manylinux1_x86                                                                                        _64.whl (13.8MB)
    100% |████████████████████████████████| 13.8MB 3.2MB/s
Installing collected packages: numpy
Successfully installed numpy-1.15.2
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

安装特定版本的包

如果我们想指定特定包运行的安装,

# pip3 install package_name==version no.

例如

# pip3 install numpy=1.15

输出

Collecting numpy==1.15
  Downloading https://files.pythonhosted.org/packages/29/b9/479ccb55cc7dcff3d4fc7c8c26d4887846875e7d4f04483a36f335bed712/numpy-1.15.0-cp35-cp35m-manylinux1_x86_64.whl (13.8MB)
    100% |████████████████████████████████| 13.8MB 101kB/s
Installing collected packages: numpy
Successfully installed numpy-1.15.0
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

用pip升级包装

使用pip运行将包升级到最新版本

# pip3 install --upgrade package_name

例如,我们从上面的输出中看到了我们运行PIP版本10.0.1.
升级到最新版本为18.1,运行

# pip3 install --upgrade pip

输出

Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 7.5MB/s
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-18.1

用pip卸载包装

用pip运行卸载包

# pip3 uninstall package_name

例如

# pip3 uninstall numpy

输出

Uninstalling numpy-1.15.2:
Would remove:
/usr/local/bin/f2py
/usr/local/lib/python2.7/dist-packages/numpy-1.15.2.dist-info/*
/usr/local/lib/python2.7/dist-packages/numpy/*
Proceed (y/n)? y

查看更多选项

查看更多PIP运行的PIP选项

# pip3 --help