Python3 ssl模塊不可用的問題


編譯安裝完Python3之后,使用pip來安裝python庫,發現了如下報錯:

$ pip install numpy
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting numpy
  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/numpy/
  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/numpy/
  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/numpy/
  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/numpy/
  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/numpy/
  Could not fetch URL https://pypi.python.org/simple/numpy/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /simple/numpy/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
  Could not find a version that satisfies the requirement numpy (from versions: )
No matching distribution found for numpy

網上說了一種解決方案,是在./configure 的時候,加上--with-ssl選項,然后重新編譯安裝,嘗試了下:

$ ./configure --with-ssl
... ...
configure: WARNING: unrecognized options: --with-ssl
... ...

出了個警告:不可識別的--with-ssl選項。

./configure --help看了下確實也沒發現這個選項,估計是版本不一致,不大想折騰這個版本問題了,決定換個思路。

嘗試安裝openssl:

$ sudo yum install openssl

安裝成功之后,重新編譯安裝,依舊報這個錯,但是在make的時候有了一些發現:

$ make
... ...
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _lzma              
_sqlite3              _ssl                  _tkinter           
readline                                    
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

可以看到,這里雖然make成功了,但是報了很多模塊缺失,查看下編譯安裝目錄下的setup.py,搜索_ssl,可以定位到如下代碼:

 843         # Detect SSL support for the socket module (via _ssl)
 844         search_for_ssl_incs_in = [
 845                               '/usr/local/ssl/include',
 846                               '/usr/contrib/ssl/include/'
 847                              ]
 848         ssl_incs = find_file('openssl/ssl.h', inc_dirs,
 849                              search_for_ssl_incs_in
 850                              )
 851         if ssl_incs is not None:
 852             krb5_h = find_file('krb5.h', inc_dirs,
 853                                ['/usr/kerberos/include'])
 854             if krb5_h:
 855                 ssl_incs += krb5_h
 856         ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
 857                                      ['/usr/local/ssl/lib',
 858                                       '/usr/contrib/ssl/lib/'
 859                                      ] )
 860 
 861         if (ssl_incs is not None and
 862             ssl_libs is not None):
 863             exts.append( Extension('_ssl', ['_ssl.c'],
 864                                    include_dirs = ssl_incs,
 865                                    library_dirs = ssl_libs,
 866                                    libraries = ['ssl', 'crypto'],
 867                                    depends = ['socketmodule.h']), )
 868         else:
 869             missing.append('_ssl')

可以看到,setup.py會在'/usr/local/ssl/include', '/usr/contrib/ssl/include/' 這兩個目錄里面搜索'openssl/ssl.h' 這個頭文件,然后會在 '/usr/local/ssl/lib' 和 '/usr/contrib/ssl/lib/' 之中搜索 ssl 的 lib文件,搜索不到,會將_ssl加入missing這個數組里面,然后尋找missing調用的地方:

 313         if missing:
 314             print()
 315             print("Python build finished successfully!")
 316             print("The necessary bits to build these optional modules were not "
 317                   "found:")
 318             print_three_column(missing)
 319             print("To find the necessary bits, look in setup.py in"
 320                   " detect_modules() for the module's name.")
 321             print()

找到了上面報錯時候的輸出,很明顯,是由於搜索不到ssl.h頭文件或者搜索不到lib文件而導致的報錯,但是我剛剛明明是裝了openssl的啊,為啥還會報找不到呢?手動搜索下:

$ sudo find / -name ssl.h

沒找到ssl.h,折騰了一番之后,找到了如下命令:

$ sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel 

可以看到,這個命令安裝的是openssl-devel,與我之前安裝的openssl有所不同,查閱資料之后發現,openssl只包含了可執行部分,openssl-devel才包含了頭文件、頭文件參考、某些庫文件等跟開發相關的東西。所以只安裝openssl是找不到相應的頭文件的,安裝完之后,再次編譯:

$ make clean
$ make
... ...
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_dbm                  _gdbm                 _lzma              
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

果然發現,缺失的模塊少了很多。

繼續安裝:

$ sudo make install

安裝完之后,重新執行pip:

$ pip install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/68/1e/116ad560de97694e2d0c1843a7a0075cc9f49e922454d32f49a80eb6f1f2/numpy-1.14.5-cp36-cp36m-manylinux1_x86_64.whl (12.2MB)
    6% |██                              | 747kB 10kB/s eta 0:17:52

至此,pip安裝報錯的問題解決。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM