centos 7 安装 python 3.7.1
https://www.python.org/downloads/source/
在官网上下载 Python-3.7.1.tar.xz 安装包,在下载时可以看到有 XZ 和 Gzip 两种格式的压缩包,这两个都可以下载使用,只是 XZ 格式的压缩包体积会更小些。
一、安装 python3 所需依赖包
[[email protected] /]# yum -y groupinstall "Development tools"
[[email protected] /]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
二、编译安装 python3.7.1 源码包
[[email protected] /]# mkdir /usr/local/python3.7.1 && cd /usr/local/python3.7.1/
[[email protected] python3.7.1]# ./configure --prefix=/usr/local/python3.7.1
[[email protected] python3.7.1]# make && make install
编译中可能会出现 ModuleNotFoundError: No module named ‘_ctypes‘ make: *** [install] Error 1 这个问题,需要安装新版本的 libffi-devel 包
[[email protected] python3.7.1]# yum install libffi-devel -y
[[email protected] python3.7.1]# make install
三、创建 python3 软链接
[[email protected] python3.7.1]# ln -s /usr/local/python3.7.1/bin/python3 /usr/bin/python3
[[email protected] python3.7.1]# ln -s /usr/local/python3.7.1/bin/pip3 /usr/bin/pip3
四、安装后测试使用 python3
[[email protected] python3.7.1]# python3
Python 3.7.1 (default, Dec 29 2018, 16:46:46)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
五、优化配置,使 python3 与 2 版本共存
[[email protected] python3.7.1]# vim /usr/bin/yum
#!/usr/bin/python2 //将原来的 python 改为 python2
[[email protected] python3.7.1]# vim /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python2 //将原来的 python 改为 python2
六、查看 python3/2 版本
[[email protected] /]# python3 -V
Python 3.7.1
[[email protected] ~]# python2 -V
Python 2.7.5
原文地址:https://www.cnblogs.com/puppet/p/10197052.html