centos7中python2.7升级到python3.9

一、下载源码包

# 切换到root目录
[root@localhost ~] cd /root/

# 安装wget
[root@localhost ~] yum -y install wget

# 使用wget下载到目录
[root@localhost ~] wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tar.xz

# 解压
[root@localhost ~] tar xvf Python-3.9.9.tar.xz

二、安装依赖

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

三、编译安装

# 切换到解压后的目录Python-3.9.9
[root@localhost ~] cd Python-3.9.9/

# 编译
[root@localhost Python-3.9.9] ./configure --prefix=/usr/local/python3.9 --enable-shared CFLAGS=-fPIC

# 生成安装文件,进行安装
[root@localhost Python-3.9.9] make && make install

四、配置环境

# 备份python软连接,pip如果不存在就不用备份
[root@localhost Python-3.9.9] mv -i /usr/bin/python /usr/bin/python.bak
[root@localhost Python-3.9.9] mv -i /usr/bin/pip /usr/bin/pip.bak

# 创建python3的连接
[root@localhost Python-3.9.9] ln -sv /usr/local/python3.9/bin/python3 /usr/bin/python
[root@localhost Python-3.9.9] ln -sv /usr/local/python3.9/bin/pip3 /usr/bin/pip

# 配置动态库
[root@localhost Python-3.9.9] vim /etc/ld.so.conf.d/python.conf
# 写入内容
/usr/local/python3.9/lib
# 启用配置
[root@localhost Python-3.9.9] ldconfig

五、解决yum和防火墙问题

# 修改下面几个文件内容的第一行的python为python2.7
[root@localhost Python-3.9.9] vim /usr/libexec/urlgrabber-ext-down
[root@localhost Python-3.9.9] vim /usr/bin/yum
[root@localhost Python-3.9.9] vim /usr/bin/firewall-cmd
[root@localhost Python-3.9.9] vim /usr/bin/firewall-offline-cmd
[root@localhost Python-3.9.9] vim /usr/sbin/firewalld

# 验证yum
[root@localhost Python-3.9.9] yum list
# 验证firewall
[root@localhost Python-3.9.9] systemctl status firewalld.service

六、修改pip源为阿里云

# 创建配置文件pip.conf
[root@localhost Python-3.9.9] 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.9.9] pip install --upgrade pip

七、校验、删除

# 命令行输入python,即可查看到版本号,如果还是2.7之类的就是没成功
[root@localhost Python-3.9.9] python
Python 3.9.9 (main, Nov  3 2022, 12:44:11) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# python命令下,按住Ctrl+D退出
[root@localhost Python-3.9.9] cd /root/

# 删除解压文件和安装包,如果需要也可以不删除
[root@localhost ~] rm -rf Python-3.9.9 Python-3.9.9.tar.xz
posted @ 2020-01-17 13:41  喵喵扑  阅读(4443)  评论(3编辑  收藏  举报