Centos7下安装python3.8

1. Centos7下安装python3.8

1.1 安装依赖包

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel

这些依赖包,用于python的源码编译安装时候使用。

1.2 下载python3.8的压缩包

wget https://www.python.org/ftp/python/3.8.16/Python-3.8.16.tgz

1.3 解压

tar -zxvf Python-3.8.16.tgz

1.4 编译安装

./configure --prefix=/home/programs/python/Python-3.8.16 --with-openssl=/usr/local/openssl

# 指定编译文件的存放目录(安装目录)
# --prefix=/home/programs/python/Python-3.8.16
# 待openssl包编译,否则pip install组件的时候,会无法下载https的组件
# --with-openssl=/usr/local/openssl
make && make install

1.5 python3的环境配置

centos7 默认安装了python2.7的环境,而且很多组件也都依赖python2.7的环境,所以我们不准备替换掉python2.7,而是让两个版本共存。这里的python3.8我们就别名为python3,当然你也可以设置为python3.8

# 设置软连接
rm -rf /usr/bin/pip3 /usr/bin/python3
ln -s /home/programs/python/Python-3.8.16/bin/pip3.8 /usr/bin/pip3
ln -s /home/programs/python/Python-3.8.16/bin/python3.8 /usr/bin/python3


# 查看软连接是否设置好了
ll /usr/bin/python*
ll /usr/bin/pip*

1.6 测试安装结果

# 查看python是否安装成功
python3 -V
pip3 -V

2. 设置python的包安装源 

python的官方包安装源在境外,所以导致pip install的时候,会非常慢。 我们可以选择国内的安装源,以此来加快安装速度。这里用的是阿里云的pypi源。

# 创建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

3. 常见问题

3.1 centos解决 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 

### 下载 openssl 编译安装  ####
# 下载并解压
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1d.tar.gz
tar -zxvf OpenSSL_1_1_1d.tar.gz

# 指定安装路径并编译
./config --prefix=/usr/local/openssl
make && make install

# 替换当前系统的旧版本 openssl 「先保存原来的」
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/lib64/openssl /usr/lib64/openssl.old
mv /usr/lib64/libssl.so /usr/lib64/libssl.so.old

ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/lib/libssl.so /usr/lib64/libssl.so
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf

# 建立动态链接
ldconfig -v 

你可能感兴趣的:(python,开发语言,centos)