python-pip 安装

翻译自本人的英文版博客 《Installing Python-pip》

In python, pip is a important package management system(or package manager) used to install and manage software packages written in python 1. There are different ways to install python-pip varies from different platforms. Please note that you have to install python in front of your installation of pip.

在python中,一般利用 pip 来管理要安装的 python 包,pip 因此可以看做是一个 python 的包管理器 (package manager)。在不同环境下,我们安装 pip 的方法不尽相同,但是前提是必须先安装了 python ,再安装 pip,安装顺序要正确。

1. 如何安装 pip?

Actually, for python version 2.7.9 and python versions 3.4+, the installation of python includes the installation of pip, i.e., you do not have to deal with the installing problem of pip when you install these version of python.

事实上对于 python 的 2.7.93.4+ 版本,pip 的安装是内嵌于 python 的安装中。也就是说当我们安装了以上的 python 版本后,我们无须再次安装 pip 了,因为 python 已经将其包含在内。那么对于其他版本的 python 用户来讲呢,就需要额外的去安装 pip 了,主要以 WindowsUbuntu 为例。

  • 在 Ubuntu 环境下,我们只需要一条简单的命令就可以实现 pip 的安装。
sudo apt-get install python-pip
  • 在 Windows 环境下,我们既可以通过官网下载 pip installer 2 来安装 pip. 同时,也可以在 CMD 窗口输入命令来进行安装 3
python get-pip.py

2. 如何使用 pip?

One of the advantage of pip is the ease of its command-line interface, which makes installation of some python packages much easier than the traditional download-and-install method. for instance, if you wanna install some packages,

pip 的一大优势就在于其使用的简单易用性。在 pip 里,我们往往只需要短短的一句话就能够实现包的自动下载和安装,这比传统的手动操作更加省力省时。

  • 我们要安装一个名为 some-package-name 的 python 包。
pip install some-package-name
  • 我们要移去一个名为 some-package-name 的 python 包。
pip uninstall some-package-name

3. 遇到的问题.

One typical problem I met when I am trying to install python-pip is the 404 Not Found problem. My python version is the old 2.7.10, which is installed on Ubuntu 15.X. When I type the command sudo apt-get install python-pip command, some important links are NOT FOUND!

很不幸,我的 python 版本既不是 2.7.9 ,也不是 3.4+,而是略显尴尬的 2.7.10。这就需要我手动来安装 pip 了,于是麻烦来了。当我敲入 sudo apt-get install python-pip 的时候,出现了下列报错,提示我如下等等链接不能找到,也就是 404 Not Found 问题。

W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/wily/multiverse/
binary-i386/Packages 404 Not Found [IP: 91.189.91.26 80]

W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/wily-backports/
restricted/source/Sources 404 Not Found [IP: 91.189.91.26 80]]

W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/wily-backports/
main/binary-amd64/Packages 404 Not Found [IP: 91.189.91.26 80]

This problem confuses me a lot of time, and I also search the related problems post on StackOverflow. such as running sudo apt-get update in terminal, etc., but all failed.

This problem let me consider about the working principle of pip and command apt-get, pip provides an package installation interface of python, and when we invoke the pip command, the whole download and installation processes will running underground and automatically. apt-get is an common command to manage softwares of Ubuntu, so we can also regard it as an installation interface of Ubuntu.

这个问题困惑了我很久,我一直在思考“为什么我安装 pip 的时候提示我链接找不到了”,即 Failed to fetch 错误。期间我也去过 StackOverflow 4 上去查找过,也尝试过 sudo apt-get update 等命令,但是都不奏效。

我又了解了一下 pipapt-get 的原理,疑惑慢慢解开。pip 一般看作是 python 包安装管理的接口,一旦执行 pip 命令,下载和安装就会在后台自动进行。apt-get 则是 Ubuntu 系统的软件下载管理的接口,在 Ubuntu 中,我们比较习惯的就是用 sudo apt-get install 命令来安装 git,maven 等软件。因此 pipapt-get 是两个不同的包/软件的管理接口。那么这个 404 Not Found 的原因就处在 Ubuntu 本身,而不在于 pip

The 404 Not Found warnings exactly proves that principle, It is the source problem that result in such installation errors, since the apt-get manager CANNOT download the package pip from the links in old sources.list. Hence, we’d better to update the file sources.list to a new version. The following 3 steps will solve the problem perfectly.

原来,每次 Ubuntu 在调用 apt-get 来下载安装软件的时候,是按照自身一套 sources.list 文件来对照链接下载软件,再安装的。随着 Ubuntu 版本不断更新,sources.list 文件也随之更新,这就会导致其中部分的链接失效,当 apt-get 再次被使用时,就会出现 Failed to fetch404 Not Found 错误。解决方法于是付出水面,那就是:及时更新 sources.list 文件!

步骤 1. 打开 sources.list 所在目录,也就是 /etc/apt/sources.list

cd /etc/apt

步骤 2. 打开 sources.list 文件,并添加 Ali 或者其他公司提供的 sources.list 内容 (具体请参见 5 ).

sudo gedit sourcces.list

步骤 3. 更新 Ubuntu 系统

sudo apt-get update

以下展示了 souces.list 部分内容 (图片来自于 [5] )
sources.list 部分内容

Finally, after modify the sources.list files, the apt-get command can catch softwares or packages following this sources list, which solving the 404 Not Found problem absolutely. Try to type command pip -V in terminal:)

总结:

  1. pip 是 python 内部的包管理工具,它会自动帮你下载并安装好所需要的 python 库。安装和使用都非常方便。
  2. apt-get 是 Ubuntu 系统的软件管理工具,它也会按照 sources.list 来帮你自动下载并安装好所需要的软件。
  3. 安装好 pip 后,可以使用 pip -V 来检测是否安装成功。

猜你喜欢

转载自blog.csdn.net/chikily_yongfeng/article/details/81058228
今日推荐