Python pip で SSLErrorが出た時の対処法

はじめに

  • 会社のPCでpipをアップグレードしようとしたらエラーが出たので、その時の内容と対処したことについて自分への備忘録のために記事としてまとめました。

環境

エラー

PS C:\Windows\system32> pip install --upgrade pip

...(略)...

  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: self signed certificate in certificate chain (_ssl.c:1123)'))': /packages/1f/2c/d9626f045e7b49a6225c6b09257861f24da78f4e5f23af2ddbdf852c99b8/pip-22.2.2-py3-none-any.whl

...(略)...

ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/1f/2c/d9626f045e7b49a6225c6b09257861f24da78f4e5f23af2ddbdf852c99b8/pip-22.2.2-py3-none-any.whl (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1123)')))

解決方法

調べてみたところ、pipのオプションで接続先を信頼済みとすればOKとのことでした。 なので、下記のように記述を足して再度実行してみたら無事にpipの更新ができました。

pip install --upgrade pip --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org

ちなみに、例えば何か(下の例ではpytest)をインストールしたい時は以下で実行することができます。

pip install pytest --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org

参考資料

qiita.com

hk29.hatenablog.jp