我的博客
欢迎来到我的博客
bunny.icu

CentOS7安装python3

CentOS7安装python3

CentOS7安装python3

下载

wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
# 或
# wget https://file-wsg.oss-cn-shanghai.aliyuncs.com/Linux/Python-3.8.2.tgz
tar -zxvf Python-3.8.2.tgz

安装

先安装依赖的包,如果有可以跳过

yum -y install gcc
yum -y install zlib*
yum -y install libffi-devel
yum -y install openssl-devel
yum -y install zlib-devel python-devel gcc libffi-devel tk-devel libpcap-devel bzip2-devel db4-devel xz-devel openssl-devel ncurses-devel patch readline-devel gdbm-devel sqlite-devel
cd Python-3.8.2
./configure --prefix=/usr/local/python3
make && make install

配置软连接

删除python2的软连接

mv /usr/bin/python /usr/bin/python2.7

如果/usr/bin/python2.7已存在,可以直接删除/usr/bin/python

rm -rf /usr/bin/python
rm -rf /usr/bin/pip

添加python3的软链接

添加软连接必须使用绝对路径。

ln -s /usr/local/python3/bin/python3.8 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

查看版本:

python -V

修改yum相关设置

因yum的功能依赖于Python2.x,更改python默认版本后会导致 yum无法正常工作,所以要修改yum

vi  /usr/bin/yum

修改第一行

#!/usr/bin/python2.7
vi /usr/libexec/urlgrabber-ext-down

修改第一行

#! /usr/bin/python2.7

常见错误

找不到ssl模块

使用pip install xxx时出现错误

Can't connect to HTTPS URL because the SSL module is not available

解决方式是修改/root/Python-3.8.2/Modules/Setup文件

vim /root/Python-3.8.2/Modules/Setup/Setup

文件中有四行内容是

#SSL=/usr/local/ssl
#_ssl _ssl.c \
#        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#        -L$(SSL)/lib -lssl -lcrypto

将注释取消,然后重新编译安装

./configure --prefix=/usr/local/python3
make && make install

Reference

Centos7下安装Python3.x替代原生Python2.x
python3遇到Can’t connect to HTTPS URL because the SSL module is not available.

版权声明


本作品系原创, 转载须遵循 CC BY-NC-ND 4.0 许可协议
本文标题:CentOS7安装python3
本文链接:https://www.bunny.icu/archives/1067

推荐文章

发表评论

textsms
account_circle
email

bunny.icu

CentOS7安装python3
CentOS7安装python3
扫描二维码继续阅读
2020-03-04