WindowsでPIP Install するとSSLエラーになるのを解消する。


発生したエラー

(ptoe) D:\MyFile\arc\pyenv\ptoe>pip install pprint
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))': /simple/pprint/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))': /simple/pprint/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))': /simple/pprint/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))': /simple/pprint/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))': /simple/pprint/
Could not fetch URL https://pypi.org/simple/pprint/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pprint/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))) - skipping
ERROR: Could not find a version that satisfies the requirement pprint (from versions: none)
ERROR: No matching distribution found for pprint
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(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)'))) - skipping

SSL認証でエラーになっている。接続先が信用できないということだが原因が何かはわからない。
社用PCなのであまりネットワーク設定をいじるのも怖い。

解決方法

ググったところ、pipのオプションで接続先を信頼済みとすればOKらしい。
(MACの例ばかりでWindowsの例が少ないのが不安だけど。)

「--trusted-host」オプションでインストール時のみ接続先のサイトを認証OKにする。
必要なのは下記3つ。足りないと上記同様のエラーとなる。

 --trusted-host pypi.python.org
 --trusted-host files.pythonhosted.org
 --trusted-host pypi.org

実践

1.PIPを更新する。

(ptoe) D:\MyFile\arc\pyenv\ptoe>python -m pip install --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --trusted-host pypi.org --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/36/74/38c2410d688ac7b48afa07d413674afc1f903c1c1f854de51dc8eb2367a5/pip-20.2-py2.py3-none-any.whl (1.5MB)
     |████████████████████████████████| 1.5MB 3.3MB/s
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-20.2

PIPのアップグレードが通った!!(´ω`)

2.エラーになっていたライブラリのインストール

(ptoe) D:\MyFile\arc\pyenv\ptoe>python -m pip install --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --trusted-host pypi.org pip pprint
Requirement already satisfied: pip in ![コメント 2020-08-04 155203.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600163/6dea129d-3250-af0c-fff9-bb139a38391f.png)
d:\myfile\arc\pyenv\ptoe\lib\site-packages (20.2)
Collecting pprint
  Downloading pprint-0.1.tar.gz (860 bytes)
Using legacy 'setup.py install' for pprint, since package 'wheel' is not installed.
Installing collected packages: pprint
    Running setup.py install for pprint ... done
Successfully installed pprint-0.1

ライブラリも正常にインストールできた。

永続化

毎回「--trusted-host」を3つも書いてインストールするのはちょっと...
という場合は、pip.iniを作成して書いとけば以降は--trusted-hostオプションを指定する必要はなくなる。

[global]
trusted-host = pypi.python.org
               pypi.org
               files.pythonhosted.org

Windowsの場合にpip.iniをどこに置けばいいか全然わからなかったけど、
pipの公式ドキュメントに書いてあった。

#ユーザーローカルの場合
%APPDATA%\pip\pip.ini

#グローバルの場合
C:\ProgramData\pip\pip.ini

#仮想環境(venv)の場合
%VIRTUAL_ENV%\pip.ini

仮想環境の格納イメージ

参考

https://github.com/pypa/pip/issues/5448
https://pip.pypa.io/en/stable/user_guide/#configuration