1、在官网下载tar.gz安装文件,上传到服务器(或者直接下载)
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
2、安装依赖包
yum -y install zlib zlib-devel yum -y install bzip2 bzip2-devel yum -y install ncurses ncurses-devel yum -y install readline readline-devel yum -y install openssl openssl-devel yum -y install openssl-static yum -y install xz lzma xz-devel yum -y install sqlite sqlite-devel yum -y install gdbm gdbm-devel yum -y install tk tk-devel yum -y install gcc(不安装这个在下一步./configure会报错) yum install libffi-devel -y(3.7以上版本需要安装)
3、解压安装包
tar -xvzf xxx.tar.gz
4、进入解压目录,配置安装目录
cd 解压目录
./configure --prefix=/usr/python3
5、编译源码、执行安装
make && make install make clean make disclean
6、系统中原来的python2.7在/usr/bin/python目录,建立软链接
若需要将默认python替换成python3
mv /usr/bin/python /usr/bin/python2.7 //当前python的版本为2.7所以是python2.7 ln -s /usr/local/bin/python3.7 /usr/bin/python
若不需要替换默认python
ln -s /usr/local/bin/python3.7 /usr/bin/python3
7、修改yum配置文件(因为yum是用的python2.7的代码),修改第一行的python为
#!/usr/bin/python2.7
其他命令有问题也可照此修改
原文:https://www.cnblogs.com/jzss/p/10840079.html