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 makepython

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'的错误sql

同时编译安装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 -lcryptocentos

将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 -lcryptosocket

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

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 -> python2ip

若是想要保留Python2,不要覆盖它,若是不想保留,直接覆盖就好,由于yum须要用到python2,本人保留python仍是指向Python2,建立一个新的软链指向python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ssl

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