6

Centos7升级Python2到Python3

 2 years ago
source link: https://segmentfault.com/a/1190000040759182
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在Centos7系统中,默认带的Python版本是Python2.7的,想用到Python3的新特性就需要升级.

安装依赖包

关于Python3.7以上的版本,需要多安装一个依赖包:

> yum install -y libffi-devel

否则会出现ModuleNotFoundError: No module named _ctypes的报错。

安装python 3.7.3

# 下载
> wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
# 解压
> tar -zxf Python-3.7.3.tgz
# 安装依赖包
> yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc  libffi-devel
# 进入python目录
> cd Python-3.7.3
# 编译
> ./configure --prefix=/usr/local/python3.7
#安装
> make && make install

备份Python2版本

由于系统有些软件还是依赖Python2运行,所以不能删除Python2,需要对原来的python软连接指向python3

>mv /usr/bin/python /usr/bin/python.bak

创建新的软连接

> ln -s /usr/local/python/bin/python3.7 /usr/bin/python

查看Python版本

> python -V

更改yum配置

因为yum需要使用python2,将/usr/bin/python改为python3后,yum就不能正常运行了,因此需要更改一下yum的配置。

  • 编辑这两个文件,将文件头的#!/usr/bin/python改为#!/usr/bin/python2
> vim /usr/bin/yum
> vim /usr/libexec/urlgrabber-ext-down

原文链接:https://rumenz.com/rumenbiji/...
微信公众号:入门小站


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK