centos7中python2.7升级到python3.7

一、下载源码包

切换到root目录

[root@localhost ~] cd /root/

安装wget

[root@localhost ~] yum -y install wget

使用wget下载到目录

[root@localhost ~] wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz

解压

[root@localhost ~] tar xvf Python-3.7.0.tar.xz

二、安装依赖

[root@localhost ~] yum install gcc openssl-devel bzip2-devel expat-devel gdbm-devel sqlite-devel libffi-devel

三、编译安装

切换到解压后的目录Python-3.7.0

[root@localhost ~] cd Python-3.7.0

编译

[root@localhost Python-3.7.0] ./configure --prefix=/usr/local/python3.7 --enable-shared CFLAGS=-fPIC

生成安装文件,进行安装

[root@localhost Python-3.7.0] make && make install

四、配置环境

备份python软连接,pip如果不存在就不用备份

[root@localhost Python-3.7.0] mv -i /usr/bin/python /usr/bin/python.bak
[root@localhost Python-3.7.0] mv -i /usr/bin/pip /usr/bin/pip.bak

创建python3的连接

[root@localhost Python-3.7.0] ln -sv /usr/local/python3.7/bin/python3 /usr/bin/python
[root@localhost Python-3.7.0] ln -sv /usr/local/python3.7/bin/pip3 /usr/bin/pip

配置动态库

[root@localhost Python-3.7.0] vim /etc/ld.so.conf.d/python.conf

写入内容

/usr/local/python3.7/lib

启用配置

[root@localhost Python-3.7.0] ldconfig

五、解决yum和防火墙问题

修改下面几个文件内容的第一行的python为python2.7

[root@localhost Python-3.7.0] vim /usr/libexec/urlgrabber-ext-down
[root@localhost Python-3.7.0] vim /usr/bin/yum
[root@localhost Python-3.7.0] vim /usr/bin/firewall-cmd
[root@localhost Python-3.7.0] vim /usr/bin/firewall-offline-cmd
[root@localhost Python-3.7.0] vim /usr/sbin/firewalld

验证yum

[root@localhost Python-3.7.0] yum list

验证firewall

[root@localhost Python-3.7.0] systemctl status firewalld.service

六、修改pip源为阿里云

创建配置文件pip.conf

[root@localhost Python-3.7.0] cd /root/ && mkdir .pip && cd .pip && touch pip.conf

把下列内容写入到pip.conf文件

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

更新pip到最新版

[root@localhost Python-3.7.0] pip install --upgrade pip

七、校验、删除

命令行输入python,即可查看到版本号,如果还是2.7之类的就是没成功

[root@localhost Python-3.7.0] python
Python 3.7.0 (default, Jan 21 2020, 09:22:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

python命令下,按住Ctrl+D退出

[root@localhost Python-3.7.0] cd /root/

删除解压文件和安装包,如果需要也可以不删除

[root@localhost ~] rm -rf Python-3.7.0 Python-3.7.0.tar.xz
  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明算科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值