Python2.7升级到Python3.7及其报错处理
系统环境说明:
系统版本 Centos7.3和Centos7.6均可
Python版本 Python2.7——》Python3.7 (其中原来的Python2.7不卸载)
安装方法:
编译安装(暂时没发现其他安装方法)
Pythony3.7安装包下载
https://www.python.org/downloads/release/python-376/
#!/bin/bash #安装相关依赖 yum install libffi-devel zlib zlib-devel gcc openssl openssl-devel wget -y #下载压缩包 wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz #解压 tar -xvf Python-3.7.6.tar.xz cd Python-3.7.6 #安装在/usr/local/python3.7目录下 ./configure --prefix=/usr/local/python3.7 make make install cp /usr/bin/python /usr/bin/python_old rm -rf /usr/bin/python #用新版本的python代替旧版本的python ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python #查看版本时候升级成功 python --version #解决升级python后,yum无法正常使用 sed -i 's/python/python_old/g' /usr/bin/yum #解决使用yum时提示except OSError, e: ^ SyntaxError: invalid syntax sed -i 's/python/python_old/g' /usr/libexec/urlgrabber-ext-down #验证yum是否能正常使用 yum list #验证yum是否能正常安装 yum install lsof -y #将新版本的python相关命令加入到系统环境中 echo 'export PATH=$PATH:/usr/local/python3.7/bin/' >> /etc/profile #使文件生效 source /etc/profile
安装好之后,有时候会报以下错误
[root@instance-u5vn5wqr rpm]# yum install lsof -y Loaded plugins: langpacks, versionlock Traceback (most recent call last): File "/usr/bin/yum", line 30, in <module> yummain.user_main(sys.argv[1:], exit_code=True) File "/usr/share/yum-cli/yummain.py", line 375, in user_main errcode = main(args) File "/usr/share/yum-cli/yummain.py", line 170, in main base.getOptionsConfig(args) File "/usr/share/yum-cli/cli.py", line 296, in getOptionsConfig (opts, self.cmds) = self.optparser.setupYumConfig(args=args) File "/usr/share/yum-cli/cli.py", line 2329, in setupYumConfig (opts, cmds) = self.parse_args(args=args) File "/usr/lib64/python2.7/optparse.py", line 1401, in parse_args self.error(str(err)) File "/usr/lib64/python2.7/optparse.py", line 140, in __str__ return _("no such option: %s") % self.opt_str UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 1: ordinal not in range(128) [root@instance-u5vn5wqr rpm]#
解决方法
在 /usr/share/yum-cli/yummain.py和 /usr/lib64/python2.4/encodings/utf_8.py 中加入三行
import sys
reload(sys)
sys.setdefaultencoding('gbk')