Centos7安装Python3并兼容Python2的方法

Centos7安装Python3的方法

因为centos7本来就安装了Python2,并且这个Python2不能被删除,由于有不少系统命令,好比yum都要用到。html

[root@VM_105_217_centos Python-3.6.2]# python
Python 2.7.5 (default, Aug  4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

输入Python命令,查看能够得知是Python2.7.5版本python

输入linux

which python

能够查看位置,通常是位于/usr/bin/python目录下。sql

安装Python3

安装依赖包

yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

下载python安装包

而后根据本身需求下载不一样版本的Python3,我下载的是Python3.6.2centos

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

若是速度不够快,能够直接去官网下载,利用WinSCP等软件传到服务器上指定位置,个人存放目录是/usr/local/application/python(记住这个目录,下面会用到)服务器

安装编译

mkdir /usr/local/application/python

创建一个空文件夹,而后解压压缩包,进入该目录,安装Python3app

tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2

说明:/usr/local/application/python 是python安装的目录,默认安装目录为/usr/local/。python2.7

若是出现如下错误提示:configure: error: no acceptable C compiler found in $PATH 测试

则表示系统没有安装gcc
安装gcc从新执行./configure –prefix=/usr/local/python3便可url

[root@centos Python-3.6.1]# yum install gcc
[root@centos Python-3.6.1]# ./configure --prefix=/usr/local/application/python
[root@centos Python-3.6.1]# make && make install

安装成功后在/usr/local/application/python目录下生成几个目录

/usr/local/application/python/bin目录下执行python3.6,出现以下输出证实Python已经安装成功能够用了

[root@centos python3]# python3.6 
Python 3.6.1 (default, Jun  3 2017, 07:13:03) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>


替换旧的python

第一步已经确认当前使用Python为2.7.5而且在/usr/bin目录下,接下来须要作的就是替换/usr/bin/python,并更改yum使用的解释器,否则yum将没法使用

重命名

先使用命令   mv python python_bak     重命名当前Python,结果以下

建立软连接

软链接bin以前的目录即为./configure时安装目录即/usr/local/application/python,  bin以后的python3不须要修改

ln -s /usr/local/application/python/bin/python3 /usr/bin/python
ln -s /usr/local/application/python/bin/pip3 /usr/bin/pip3

配置环境变量

让系统默认使用Python 3.6.1

在etc/profile最后一行中加入变量

export PATH="$PATH:/usr/local/python3/bin"

在命令行中输入python3测试

 

 

解决升级Python后yum,Firewall等不能用的问题

一、 升级Python后运行yum会报错

 

vi /user/bin/yum 编辑yum的执行文件,更改首行的/usr/bin/python 为上面重命名的名字或/usr/bin/目录下指向Python2.7的名字,以后再运行yum就正常了

二、 运行yum安装其余软件可能出现如下错误,这也是由于Python版本问题,
vi /usr/libexec/urlgrabber-ext-down 修改第一行的Python为重命名的名字(指向python2.7的)

File "/usr/libexec/urlgrabber-ext-down", line 28
   except OSError, e:
                 ^

三、防火墙没法启动

出现以上状况根据提示修改/usr/bin/firewall-cmd文件,顺便将同目录下的firewall-offline-cmd文件进行修改,修改后执行启动防火墙命令,会发现依然没法使用

[root@centos ~]# systemctl start firewalld.service
Job for firewalld.service failed because the control process exited with error code. 
See "systemctl status firewalld.service" and "journalctl -xe" for details.```
根据提示执行systemctl status firewalld.service命令查看状态,根据显示中红色部分能够看到,
在执行/usr/sbin/firewalld文件时出现问题,由于查看改文件

能够看到这个文件也是用Python编译的,因此也要修改
`vi /usr/sbin/firewalld`

!/usr/bin/python -Es

-- coding: utf-8 --

 

Copyright (C) 2010-2016 Red Hat, Inc.

Authors:

Thomas Woerner twoerner@redhat.com

修改完成后再次执行启动命令就能够正常启动了,神清气爽啊,哈哈哈

[root@centos ~]# systemctl start firewalld.service
[root@centos ~]# systemctl status firewalld.service
â—� firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2017-06-07 05:38:13 CST; 6s ago
Docs: man:firewalld(1)
Main PID: 2481 (firewalld)
CGroup: /system.slice/firewalld.service
└─2481 /usr/bin/python2.7 -Es /usr/sbin/firewalld --nofork --nopid

Jun 07 05:38:13 centos systemd[1]: Starting firewalld - dynamic firewall daemon...
Jun 07 05:38:13 centos systemd[1]: Started firewalld - dynamic firewall daemon.

除此以外还有一些其余文件使用的Python,可使用grep命令查看哪些文件使用了Python解释器,根据具体状况修改。
例如查看/usr/bin目录下的文件

[root@centos ~]# grep -ai /usr/bin/python /usr/bin/*
/usr/bin/easy_install:#!/usr/bin/python
/usr/bin/easy_install-2.7:#!/usr/bin/python
/usr/bin/firewall-cmd:#!/usr/bin/python2.7 -Es
/usr/bin/firewall-offline-cmd:#!/usr/bin/python2.7 -Es
/usr/bin/msghack:#!/usr/bin/python2.7
/usr/bin/pip:#!/usr/bin/python
/usr/bin/pip2:#!/usr/bin/python
/usr/bin/pip2.7:#!/usr/bin/python
/usr/bin/pydoc:#!/usr/bin/python2.7
/usr/bin/systemd-sysv-convert:#!/usr/bin/python2.7
/usr/bin/urlgrabber:#!/usr/bin/python2.7 -t
/usr/bin/wheel:#!/usr/bin/python2.7
/usr/bin/yum:#!/usr/bin/python2```

5、make install过程当中出现报错解决

出现如下问题:
FAIL: test_with_pip (test.test_venv.EnsurePipTest)
zipimport.ZipImportError: can't decompress data; zlib not available

解决办法:出现这种错误通常由于Python的安装环境没有安装好,安装如下软件
yum install openssl-devel
若是仍是有报错则安装Development tools
yum groupinstall Development tools 安装成功后make clean如下,再次重复 上面操做

相关文章
相关标签/搜索