centos 安装python3与Python2并存,并解决"smtplib" object has no attribute 'SMTP_SSL'的错误

1.需要先安装python3依赖的包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

2.安装python-3.6.8
2.1 获取python-3.6.8
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
2.2 解压&进入目录
tar -xvJf  Python-3.6.8.tar.xz
cd  Python-3.6.8

2.3 添加ssl库,如果不需要ssl库,可以选择跳过,后续需要用到的时候,再回来修改setup文件重新编译安装也是可以的,我是需要用到ssl来发邮件,所以在这里直接安装了。
关于ssl库,这里有个地方需要注意的,如果系统没有安装ssl模块,或者不清楚是否有安装的,则要在安装的时候,需要同时编译安装ssl模块,否则后续如果无法使用该模块,比如 在使用smtplib SMTP_SSL发送邮件的时候,会出现 "smtplib" object has no attribute 'SMTP_SSL'的错误

同时编译安装ssl,修改一下Modules/Setup.dist,大概在210行左右
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
#_ssl _ssl.c \
# -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
# -L$(SSL)/lib -lssl -lcrypto

将ssl的下面4个注释去掉,修改后的结果为:
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

2.4 编译&安装
./configure prefix=/usr/local/python3
make && make install

3.创建Python3到系统执行目录 /usr/bin
/usr/bin目录下有个python的执行文件,ls看一下发现它是指向系统默认安装的python2
[root@VM_0_15_centos ~]# ls -an /usr/bin/python
lrwxrwxrwx 1 0 0 7 Mar 19 2018 /usr/bin/python -> python2

如果想要保留Python2,不要覆盖它,如果不想保留,直接覆盖就好,因为yum需要用到python2,本人保留python还是指向Python2,创建一个新的软链指向python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3

到此Python3就安装完成了,使用python3 -V就可以查看python3版本了,运行python脚本的时候,使用python3 xxx.py就可以执行python3的脚本了,而使用Python xxx.py就还是使用Python2来运行脚本。

猜你喜欢

转载自www.cnblogs.com/vathena/p/10630055.html