安装依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
进入安装目录
cd /opt
下载 Python3.8 的压缩包
wget https://www.python.org/ftp/python/3.8.16/Python-3.8.16.tgz tar -zxvf Python-3.8.16.tgz mv Python-3.8.16 python3.8
编译安装
cd python3.8 ./configure --prefix=/opt/python3.8 --with-openssl=/usr/local/openssl make && make install
Python3 的环境配置
Centos7 默认安装了 Python2.7 的环境,而且很多组件也都依赖 Python2.7 的环境,所以我不准备替换掉 Python2.7,而是让两个版本共存。这里的 Python3.8 我们就别名为 python3,当然你也可以设置为 python3.8。
设置软链接
rm -rf /usr/bin/pip3 /usr/bin/python3 ln -s /opt/python3.8/bin/pip3.8 /usr/bin/pip3 ln -s /opt/python3.8/bin/python3.8 /usr/bin/python3
查看软链接
ll /usr/bin/python* ll /usr/bin/pip*
测试 Python
python3 -V pip3 -V
配置安装源
Python 的官方包安装源在境外,所以导致 pip install
的时候,会非常慢。我们可以选择国内的安装源,以此来加快安装速度。这里用的是阿里云的源。
创建 pip 的配置文件
mkdir -p ~/.pip vim ~/.pip/pip.conf
配置 pip.conf
[global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com