Linux Python3.7+ 執行 pip 時 SSLError 錯誤
in Linux
在用 Centos7 安裝 Python3.7+ 一般來說都沒有任何的問題
只要從原始碼編譯安裝就可以直接使用 Python3.7+ 了
但是如果要用 pip 安裝(搜尋)套件的時候就會發生錯誤
學長之前有問過我這個問題,想不到自己也碰到了
雖然學長的做法是把 Python 的版本下降到 3.6 解決這個問題
但是因為 3.6 跟 3.7 在 async/await 的寫法上不一樣
3.7 的寫法比起 3.6 更精簡,所以才想要在自己的伺服器上配置 Python3.7
(很多 Linux 自帶 Python2 而 3 之後的版本都要自行安裝)
然後如果編譯安裝會跳出以下提示
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_ssl _uuid
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the
然後查了 Centos7 的 openssl 版本….
恩…很好 1.0.2k 照理說是符合的
結果就是文字遊戲,上述的提示是指 openssl 版本要”超過” 1.0.2 或 1.1
並不是說最低要求 1.0.2 or 1.1
所以從 openssl 下載最新的版本 openssl-1.1.1
# 下載 openssl-1.1.1 原始碼
# 更多版本請直接到 https://www.openssl.org/source/ 查看
wget https://www.openssl.org/source/openssl-1.1.1.tar.gz
# 下載編譯時所需要的套件
yum install -y zlib zlib-devel openssl-devel sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++
# 解壓編譯下載的 openssl
tar zxf openssl-1.1.1.tar.gz
cd openssl-1.1.1
./config shared zlib --prefix=/usr/local/openssl
make
make install
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v
# 安裝好 openssl 1.1.1 之後要重新編譯 Python
./configure --prefix=/user/local/python37 --with-openssl=/usr/local/openssl
# 在 make 之後有可能發生錯誤 https://bugs.python.org/issue30090
# 簡單解釋是:
# make 完成之後發現回傳訊息如下
# Failed to build these modules:
# _ctypes
# 此時就要安裝必要套件
# Centos: yum install -y libffi libffi-devel
# Ubuntu: apt-get install libffi-dev
# 安裝完成後再重新 make 一次就不會有錯誤了
make
make install
編譯安裝好之後應該就可以執行 pip 不會出現 SSLError 錯誤了