1. 安装编译环境等
$ yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel python-pip
2. 升级 openssl
Python 从 3.7 版本开始要求 openssl 1.0.2+ 版本,CentOS 中默认是 openssl 1.0.1e,所以需要升级。
如果不升级,使用 pip3 install xxx
时会报错 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
升级 openssl 参考:CentOS 7 编译安装 openssl-1.1.1
3. 编译安装 Python 3.7
Python 源码下载地址:https://www.python.org/downloads/source/
下载、解压 Python 3.7 源码:
$ wget https://www.python.org/ftp/python/3.7.12/Python-3.7.12.tar.xz
$ tar -xf Python-3.7.12.tar.xz
编译安装:
$ cd Python-3.7.12
$ ./configure --prefix=/usr/local/python3.7 --with-openssl=/usr/local/openssl # 此处需要指定 openssl 的安装路径
$ make && make install # 编译安装
配置环境变量:
打开 /etc/profile
,在行尾写入:
export PATH=$PATH:/usr/local/python3.7/bin
退出编辑器后,使该文件立即生效:
$ source /etc/profile
验证是否安装成功:
$ python3 -V
$ pip3 -V