python3 install and migration
yum 安装程序有时会报错: This system is not registered with an entitlement server.
1.下载 Python3.6 的源码包
wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
2.解压源码包
tar xf Python-3.6.9.tgz
3.编译安装
cd Python-3.6.9
./configure
当运行到这里会提示没有c编译器:
安装gcc有很多种方法,参考 https://blog.csdn.net/xllntld/article/details/108918629
检查并删除原有的yum源
直接使用yum install gcc安装会提示系统未注册:
所以我们需要删除原有的yum源,换成免费版的yum源:
检查yum源:
rpm -qa | grep yum
删除原有的yum源:
rpm -qa|grep yum|xargs rpm -e --nodeps
3.2 下载安装CentOS的yum源
下载时我发现好多国内的yum源都不能用了,像网易163和清华,我这里使用的是阿里云的yum源
3.2.1 下载安装yum包
打开 https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/
下载标记的三个包:
安装:
rpm -ivh yum-*
测试:
rpm -qa |grep yum
3.2.2 备份已有的软件源
mv /etc/yum.repos.d /etc/yum.repos.d.bak
mkdir /etc/yum.repos.d
3.2.3 创建阿里云 repo
vi /etc/yum.repos.d/aliyun-base-epel.repo
---------------------复制以下内容--------------------------
[base]
name=CentOS-7 - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-7 - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-7 - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[epel]
name=CentOS-7 - Epel - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
---------------------------end----------------------------
或者参考wiki : https://www.cnblogs.com/jidehuijia/p/12957203.html
3.2.4 更新缓存
yum clean all
yum makecache
3.3 安装gcc
yum install gcc
3.4 编译安装 Python3
安装依赖环境
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的解压目录
cd Python-3.6.9
./configure
make
make install
3.5 验证安装结果
python3 -V
4 修改默认的python版本
vi /etc/profile.d/python.sh
在文本中输入下面的内容并保存 alias python='/usr/local/bin/python3.6'
alias pip='/usr/local/bin/pip3.6'
修改权限
chmod 755 /etc/profile.d/python.sh
source /etc/profile.d/python.sh
更新python软连接(非常重要,如果不更新软连接升级过程中将仍然使用python2)
rm /usr/bin/python
ln -s /usr/local/bin/python3 /usr/bin/python
5 测试
python -V
转换工具
1 工具选择
下图对比了几种工具的区别:
由于我们要保证python2和3的兼容,综合考虑我们选用了Python-Future + Futurize.
2 工具用法
安装future:
pip install future
基本用法:
futurize --stage1 -w *.py
futurize --stage2 -w *.py
3 总结
Futurize 工具确实可以帮我们转换一些基本的语法,但是不全面,一些细节的转换还是需要我们手动去调试更改。