CentOS7.6安装Python3.9

1、一先更新系统,一般使用yum更新,手动做不完的

yum update -y

2、python 安装包下载地址

wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
or
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz

3、解压文件

tar -zxvf Python-3.9.0.tgz
or
xz -d Python-3.9.0.tar.xz
tar xf Python-3.9.0.tar

4、进入Python3.9.0目录

cd Python3.9.0

5、执行配置

./configure

6、执行编译

make
这里可能会出现问题,需要安装一些包具体更具返回提示操作,因为系统检查是否满足编译环境依赖还有编译软件。
如果返回看到success 字样则可以进入下一步
解决办法:
yum install gcc ....

6、安装

make install
这里也还可能会出现问题
File "", line 523, in _get_decompress_func zipimport.ZipImportError: can't decompress data; zlib not available During handling of the above exception, another exception occurred:
具体解决办法
yum install zlib zlib-devel -y

再次编译安装直到成功安上

安装完成后发现还是python2.7
参考下面的文档配置一个虚拟环境即可

开启yum的缓存功能,yum安装软件包是在线安装,开启缓存功能可以使得软件包被下载到本地,方便后续使用。

[root@registry ~]# vim /etc/yum.conf
keepcache=1

配置网络yum源

[root@registry ~]# cd /etc/yum.repos.d/
[root@registry yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo

安装python3的依赖包

[root@registry ~]# yum install libffi-devel wget sqlite-devel xz gcc atuomake zlib-devel openssl-devel epel-release git -y

编译安装python3.9.0

[root@registry ~]# cd /usr/local/src/
[root@registry src]# rz

[root@registry src]# ll
总用量 17560
-rw-r--r-- 1 root root 17977808 9月 25 23:56 Python-3.9.0.tar.xz

[root@registry src]# tar xf Python-3.9.0.tar.xz
[root@registry src]# cd Python-3.9.0/
[root@registry Python-3.9.0]# ./configure && make install

可以看到python3.9已经安装上去了,但是默认的还是2.7.5

[root@registry ~]# python --version
Python 2.7.5
[root@registry ~]# python
python python2.7 python3.9 python3-config
python2 python3 python3.9-config

由于centos7.6自带的是python2.7.5,现在安装python3.9.0,在使用python安装软件时可能会有冲突。

一个比较好的解决方法是:进入python3虚拟环境中安装软件。

[root@registry ~]# python3 -m venv py3 安装虚拟环境,会在当前目录下生成一个py3目录
[root@registry ~]# ll py3
总用量 4
drwxr-xr-x 2 root root 193 10月 14 22:33 bin
drwxr-xr-x 2 root root 6 10月 14 22:33 include
drwxr-xr-x 3 root root 23 10月 14 22:33 lib
lrwxrwxrwx 1 root root 3 10月 14 22:33 lib64 -> lib
-rw-r--r-- 1 root root 75 10月 14 22:33 pyvenv.cfg
[root@registry ~]# source /usr/local/bin/py3/bin/activate 激活py3虚拟环境
(py3) [root@registry ~]# python -V
Python 3.9.0

有一个麻烦点的地方就是,每次在使用python3.9时,都要先激活py3虚拟环境。

解决流程

中间遇到几个问题就是依赖问题,都需要安装依赖包

原文地址:https://www.cnblogs.com/qianxiaoruofeng/p/14091786.html