背景:
今天在Linux上使用paramiko模塊的時候,出現了錯誤:ModuleNotFoundError:No module name '_ssl',但是我的系統是安裝了openssl的1.0.1的,查了網絡上的信息發現,Python3.7以后的版本,需要openssl1.0.2+,或者Libressl2.6.4+。
按照網絡上的方法,安裝了openssl-1.1.1g,對Python3.8重新手動編譯安裝,但是在執行make命令的時候仍舊提示_ssl模塊沒有被成功導入。經過查詢,發現是LDFLAGS,CPPFLAGS,PKG_CONFIG_PATH這幾個環境變量的問題。
LDFLAGS:gcc 等編譯器會用到的一些優化參數,也可以在里面指定庫文件的位置。用法:LDFLAGS=-L/usr/lib -L/path/to/your/lib。每安裝一個包都幾乎一定的會在安裝目錄里建立一個lib目錄。如果明明安裝了某個包,而安裝另一個包時,它愣是說找不到,可以把那個包的lib路徑加入的LDFALGS中試一下。
CPPFLAGS:CXXFLAGS=$CFLAGS 。CFLAGS 表示用於 C 編譯器的選項,CXXFLAGS 表示用於 C++ 編譯器的選項。這兩個變量實際上涵蓋了編譯和匯編兩個步驟。大多數程序和庫在編譯時默認的優化級別是”2″(使用”-O2″選項)並且帶有調試符號來編 譯,也就是 CFLAGS=”-O2 -g”,.
PKG_CONFIG_PATH:它指定pkg-config
將在其中搜索其.pc文件的其他路徑。此變量用於增強pkg-config的默認搜索路徑。在典型的Unix系統上,它將搜索目錄/usr/lib/pkgconfig
和/usr/share/pkgconfig
。這通常包括系統安裝的模塊。但是,某些本地模塊可能安裝在不同的前綴中,例如/usr/local
。在這種情況下,必須預先設置搜索路徑,以便pkg-config可以找到.pc文件。pkg-config
程序用於檢索有關系統中已安裝庫的信息。 pkg-config
的主要用途是提供編譯程序和鏈接到庫的必要細節。此元數據存儲在pkg-config文件中。這些文件具有后綴.pc,並位於pkg-config工具已知的特定位置。
還有可能在使用pip安裝的時候,報錯ssl module in Python is not available,這些本質上都是因為Python在編譯安裝的時候,沒有找到合適版本的ssl導致的。解決方案都是一樣的。
1 [root@localhost ~]# /usr/local/python3/bin/pip3 install paramiko 2 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 3 Collecting virtualenv 4 Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/ 5 Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/ 6 Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/ 7 Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/ 8 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenv/ 9 Could not fetch URL https://pypi.org/simple/virtualenv/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/virtualenv/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping 10 Could not find a version that satisfies the requirement virtualenv (from versions: ) 11 No matching distribution found for virtualenv 12 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 13 Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
解決方案:
需要升級openssl版本>=1.0.2或者Libressl>=2.6.4,然后對Python3.8重新編譯安裝。
1.下載openssl最新版本
1 wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
2.編譯安裝openssl
1 tar -zxvf openssl-1.1.1g.tar.gz #解壓 2 3 cd openssl-1.1.1g 4 5 ./config –prefix=/usr/local/openssl no-zlib #安裝到這個路徑 6 7 8 make 9 10 make install
3.備份原來的配置
mv /usr/bin/openssl /usr/bin/openssl.bak mv /usr/include/openssl/ /usr/include/openssl.bak
4.配置新版本的鏈接
1 #將安裝好的openssl 的openssl命令軟連到/usr/bin/openssl 2 ln -s /usr/local/openssl/include/openssl /usr/include/openssl 3 4 #軟鏈到升級后的libssl.so 5 ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so 6 7 #將安裝好的openssl命令軟連到/usr/bin/openssl 8 ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
5.修改系統配置
1 #寫入openssl庫文件的搜索路徑 2 echo "/usr/local/openssl/lib" >> /etc/ld.so.conf 3 4 #使修改后的/etc/ld.so.conf生效 5 ldconfig -v
6.查看openssl版本
openssl version
如果安裝成功的話,這時候會顯示你安裝的版本。
7. 配置環境變量
1 export LDFLAGS=" -L/usr/local/openssl/lib" 2 export CPPFLAGS=" -I/usr/local/openssl/include" 3 export PKG_CONFIG_PATH="/usr/local/openssl/lib/pkgconfig"
8.解壓python安裝包,執行configure文件
1 ./configure --enable-shared --with-openssl=/usr/local/openssl
這時候需要檢查一下最后的ssl配置是否正常,
9.安裝Python
1 make 2 3 make install
10.驗證是否成功
現在已經不報錯了,說明安裝成功了。