1. 下载python源码包
进入 https://www.python.org/downloads/source/ 选择相应版本下载到 centos主机。
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
2. 下载完成之后解压缩
tar -xvf Python-3.7.4.tgz
3. 安装编译依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel xz-devel gdbm gdbm-devel libffi-devel
The necessary bits to build these optional modules were not found:
_dbm _gdbm _lzma
xz-devel gdbm gdbm-devel 解决以上报错问题。
libffi-devel 解决以下问题:
Failed to build these modules:
_ctypes
4. 生成 Makefile 文件
cd Python-3.7.4
./configure --prefix=/usr/local/python3 --with-ssl
5. 编译安装
make && make install
6. 配置环境变量(python和pip的)
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/python3/bin/python3 /usr/bin/python
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
7. 验证python
如下图示所示,系统默认安装的pyhton2 已经被修改为 python3 了
8. yum 配置
centos yum 依赖系统安装的 python2.7 ,所以需要修改yum 相关配置文件。
修改 /usr/libexec/urlgrabber-ext-down 、 /usr/bin/yum,将 python 改为 python2.7 ,然后执行 yum 命令就可以运行了。
来源:https://blog.csdn.net/Saladbobo/article/details/100539429