RHEL7 Install Python3 and pip3

别等时光非礼了梦想. 提交于 2020-04-13 11:59:38

【今日推荐】:为什么一到面试就懵逼!>>>

方式一:yum安装python3 and pip3

添加EPEL源

RHEL7官方源里并没有python3,所以要配置下第三方源,目前EPEL仓库里最高版本只有python3.4.3,想要体验python3.5只能源码安装了,请看方法二。 以使用阿里镜像源的EPEL为例:

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum -y update

安装python3

[root@localhost ~]# yum -y install python34

okay! 通过EPEL源安装确实省了很多时间,验证一下python3:

[root@localhost ~]# python3
Python 3.4.3 (default, Jan 26 2016, 02:25:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>> 

yum安装pip3

即使是RPEL源里也是没有pip3的存在,GG了一下,网上前人的方案是使用easy_install来安装pip3。 easy_install对应的包是python34-setuptools,EPEL里就有:

[root@localhost ~]# yum install -y python34-setuptools
[root@localhost ~]# easy_install-3.4 pip

验证一下pip3是否安装正确:

[root@localhost ~]# pip3 --version
pip 8.1.1 from /usr/lib/python3.4/site-packages/pip-8.1.1-py3.4.egg (python 3.4)

okay!

方式二:源码安装python3.5

安装必要的环境

很必要,我就是图快只安装了gcc编译python3.5,然后后面python3 setup.py install安装setuptools的时候报错RuntimeError: Compression requires the (missing) zlib module,提示找不到zlib模块,SO给出解决方案是先安装zlib,再编译Python3.5,所以,步骤一定不要错了。踩坑折腾了好久,最后知道真相的我很是郁闷。。。

[root@localhost ~]# yum groupinstall "Development tools"
[root@localhost ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-deve

下载Python3.5源文件

解压并进入目录,如果你没有安装开发环境大礼包,RHEL7 Minimal的wget,unzip等工具默认是没有安装的。

[root@localhost ~]# wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
[root@localhost ~]# tar xf Python-3.5.1.tgz
[root@localhost ~]# cd Python-3.5.1

编译并安装

[root@localhost Python-3.5.1]# ./configure --prefix=/usr/local/ --enable-shared
[root@localhost Python-3.5.1]# make
[root@localhost Python-3.5.1]# make install

还差一步

让库起作用,不然输入python3.5是无法进入交互模式的,如下报错提示:

[root@localhost Python-3.5.1]# python3
python3: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

解决方案,ldconfig是让配置生效:

[root@localhost Python-3.5.1]# echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
[root@localhost Python-3.5.1]# ldconfig

Python3.5安装完成

验证一下:

[root@localhost ~]# python3
Python 3.5.1 (default, Apr 21 2016, 22:12:05) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
>>>

easy_install-3.5 和 pip3

源码安装python已默认安装了easy_install和pip工具。 验证一下:

[root@localhost ~]# easy_install-3.5 --version
setuptools 18.2
[root@localhost ~]# pip3 --version
pip 7.1.2 from /usr/local/lib/python3.5/site-packages (python 3.5)

okay!

**注意:**既然python3.5版本源码安装已经自带了easy_install和pip3倒是方便了很多,不过既然是学习,我还是记录下pip的安装过程,下面内容非必要。

源码安装easy_install和pip

当然,你也可以使用yum安装easy_install,然后通过easy_install安装pip,请看方法一。 PyPI下载setuptools最新版:

[root@localhost ~]# wget https://pypi.python.org/packages/source/s/setuptools/setuptools-20.9.0.zip#md5=38829b40023ecf141da50a3b35168b35

解压安装:

[root@localhost ~]# unzip setuptools-20.9.0.zip
[root@localhost ~]# cd setuptools-20.9.0
[root@localhost setuptools-20.9.0]# python3 setup.py install
[root@localhost setuptools-20.9.0]# easy_install-3.4 pip

如果是Python2.7版本安装:

[root@localhost setuptools-20.9.0]# python2 setup.py install
[root@localhost setuptools-20.9.0]# easy_install-2.7 pip

注意:如果提示以下错误,请安装必要环境再重新编译python3.5,顺序一定不要错:

... ...
RuntimeError: Compression requires the (missing) zlib module

验证一下:

[root@localhost ~]# pip3 --version
pip 8.1.1 from /usr/lib/python3.4/site-packages/pip-8.1.1-py3.4.egg (python 3.4)
[root@localhost ~]# pip2 --version
pip 8.1.1 from /usr/lib/python2.7/site-packages/pip-8.1.1-py2.7.egg (python 2.7)

okay!到此结束。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!