2018年2月16日 星期五

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

venv建立新環境後,安裝套件又發生問題

[root@python newproject]# ./bin/pip3 install requestspip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.Collecting requests  Could not fetch URL https://pypi.python.org/simple/requests/: 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 requests (from versions: )No matching distribution found for requests

參考以下網站,為Python 3.6增加SSL能力(https://stackoverflow.com/questions/41489439/pip3-installs-inside-virtual-environment-with-python3-6-failing-due-to-ssl-modul)

(1) 安裝openssl套件
yum install -y openssl openssl-devel (CentOS 7.4)

(2) 修改Python編譯設定檔
cd /usr/src/Python-3.6.4
./configure
vi ./Module/Setup (uncomment below lines #209~212)

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

make
make altinstall


[Python 3.6] Error: Command '['/home/newproject/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1


python3.6下使用venv建立新專案環境時發生錯誤:

[root@python Home]# python3.6 -m venv /home/newproject

Error: Command '['/home/newproject/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

經搜尋,了解python3.6預設未安裝pip,且缺了ensurepip模組
(https://github.com/ContinuumIO/anaconda-issues/issues/6917)

實際執行模組確認,發生以下問題
[root@python Home]# python3.6 -m ensurepip.....(略).....
zipimport.ZipImportError: can't decompress data; zlib not available

再經過一番搜尋,
確認必須重編python3.6支援zlib解壓縮
(網路上有其他論壇寫到./configure --with-zlib-dir=... ,此參數試過不支援)
參考下方網頁Alex Zhou的作法,重新編譯
(http://zhoujianghai.iteye.com/blog/1521993):

注意:最後步驟我改為make altinstall,保留MacOS的Python2.7,

cd /usr/src/Python-3.6.4 (我的source解開後的位置)
./configurevi Modules/Setup
(找到下面這段,將註解符號去除讓它生效)
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
make
make altinstall
完成!