[已解决]pip is configured with locations that require TLS/SSL, however the ssl module in Python is not

一、问题背景

CentOS系统上需要通过Python脚本操作MySQL数据库,脚本中需要import pymysql、requests、ssl等Python库,准备通过pip安装。

系统默认已安装Python,版本信息如下:

CentOS release 6.5 (Final)

Python 2.6.6

但因为种种原因,数据库脚本需要使用Python3,而系统yum等命令又依赖于原2.6.6版本,所以造成系统上需要Python 2.6.6和3两个版本并存本文就是介绍如何在上述系统背景下,再安装支持ssl的Python3

 

二、问题描述

第一遍编译安装Python3.6的步骤如下:

#先安装相关包:

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

#下载并编译Python3:

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

tar -xvJf  Python-3.6.2.tar.xz

cd Python-3.6.2

./configure prefix=/usr/local/python3

make && make install

mv /usr/bin/python /usr/bin/python.bak  #备份原Python

ln -s /usr/local/python3/bin/python3 /usr/bin/python  #让Python命令指向新编译的python3

python -V  #确认该命令是否指向python3

python2 -V  #确认该命令是否指向原python2.6.6

因为执行yum需要python2版本,所以我们还要修改yum的配置,执行:

vi /usr/bin/yum

把#! /usr/bin/python修改为#! /usr/bin/python2

同理修改/usr/libexec/urlgrabber-ext-down

-------------------------------------------------------------------------

到此,Python3确实是安装成功了,也能与2.6.6版并存。

但接下来通过pip(注:因为是Python3,所以实际使用pip3命令)安装pymysql、requests时提示错误:

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

#pip3 install pymysql
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting pymysql
  Could not fetch URL https://pypi.python.org/simple/pymysql/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
  Could not find a version that satisfies the requirement pymysql (from versions: )
No matching distribution found for pymysql

网上查找资料,第一种方案提示说,需要在make前,./configure那一步加一个“--with-ssl”的参数,但实际执行"./configure --with-ssl"后提示“configure: WARNING: unrecognized options: --with-ssl”。

==============划重点!==============

后来找到第二种方案,实测可行,需要在make前修改Modules/Setup文件,完整编译步骤如下

cd Python-3.6.2

make clean  #如果已经和我一样编译过一次,建议先执行此步骤,否则可省略。

./configure prefix=/usr/local/python3

修改./Modules/Setup文件(将如下四行的注释去掉):

# 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

此外,因为用到的lib路径是/usr/local/ssl,而默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件,所以还需要做以下事情:

vim /etc/ld.so.conf(这个文件记录了编译时使用的动态链接库的路径)

增加一行:/usr/local/ssl/lib(注:也可能已有,只需把双引号去掉)

并执行 /sbin/ldconfig -v命令

make && make install

到此Python3重新编译成功,并且再通过pip3安装pymysql、requests等库就不会提示如标题的错误了。

最后,执行Python操作数据库的脚本,验证一下环境是否正确。

 

 

 

 

你可能感兴趣的:(数据库,Python)