[已解決]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操作數據庫的腳本,驗證一下環境是否正確。

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章