CentOS7环境下安装python3

1、安装python3.6可能使用的依赖

yum -y install bzip2-devel expat-devel gdbm-devel readline-devel zlib-devel openssl-devel

安装openssl-devel失败,存在如下错误,不影响python3的安装但会影响pip的使用

openssl-devel

解决openssl-devel安装失败问题,可通过离线安装openssl解决,详细见:https://www.jianshu.com/p/b3a417805aa9

2、下载python源码包,地址为https://www.python.org/downloads/source/,然后通过xftp上传上去,或者直接使用wget命令,wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz下载到本地

# tar -zxvf Python-3.6.1.tgz

# cd Python-3.6.1/

安装openssl-devel失败时,需离线安装openssl,详细见:https://www.jianshu.com/p/b3a417805aa9

离线安装openssl后,进入Python-3.6.1/Modules目录,找到Setup和Setup.dist文件,将下面几行注释去掉。

Setup


Setup.dist

3、进入Python-3.6.1目录,把python3.6安装到/usr/local目录下

# ./configure --prefix=/usr/local/

# make

# make altinstall

4、更改/usr/bin/python链接

# cd /usr/bin

# mv python python.backup

# ln -s /usr/local/bin/python3.6 /usr/bin/python

# ln -s /usr/local/bin/python3.6 /usr/bin/python3

# rm -rf /usr/bin/python2

# ln -s /usr/bin/python2.7 /usr/bin/python2

5、更改yum脚本的python依赖

# cd /usr/bin

# ll yum*

有以下文件:

-rwxr-xr-x 1 root root 802 Jan 2 17:53 yum

-rwxr-xr-x 1 root root 9374 Jan 2 17:53 yum-builddep

-rwxr-xr-x 1 root root 8091 Jan 2 17:53 yum-config-manager

-rwxr-xr-x 1 root root 7610 Jan 2 17:53 yum-debug-dump

-rwxr-xr-x 1 root root 7904 Jan 2 17:54 yum-debug-restore

-rwxr-xr-x 1 root root 10906 Jan 2 17:54 yumdownloader

-rwxr-xr-x 1 root root 11032 Jan 2 17:54 yum-groups-manager

修改以上文件头:#!/usr/bin/python 改为 #!/usr/bin/python2

可以安装一个tree测试一下

# yum -y install tree

如果出现以下报错:File “/usr/libexec/urlgrabber-ext-down”, line 28

vi /usr/libexec/urlgrabber-ext-down

将/usr/bin/python改为/usr/bin/python2即可

这样,yum安装软件的时候就不会报错了,而且默认安装的就是python3了,输入python时,就会进入python3界面,如果想进入python2,那么就输入python2好了。

参考文章:
https://www.cnblogs.com/xiaoyafei/p/8321015.html
https://www.cnblogs.com/hanfe1/p/13071020.html

你可能感兴趣的:(CentOS7环境下安装python3)