Anacondaの仮想環境でpip installに失敗する際の対応

anaconda_pip-install_failer Python

はじめに

Anaconda上に構築した仮想環境にpip installをすると以下のエラーが発生したため,対応方法を残します。

>pip install jupyter
pip_system_certs: ERROR: could not register module: No module named 'wrapt'
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:1000)'))': /simple/jupyter/
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: self-signed certificate in certificate chain (_ssl.c:1000)'))': /simple/jupyter/
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: self-signed certificate in certificate chain (_ssl.c:1000)'))': /simple/jupyter/
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: self-signed certificate in certificate chain (_ssl.c:1000)'))': /simple/jupyter/
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: self-signed certificate in certificate chain (_ssl.c:1000)'))': /simple/jupyter/
Could not fetch URL https://pypi.org/simple/jupyter/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/jupyter/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)'))) - skipping
ERROR: Could not find a version that satisfies the requirement jupyter (from versions: none)
ERROR: No matching distribution found for jupyter
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: self-signed certificate in certificate chain (_ssl.c:1000)'))) - skipping

試行錯誤したこと

会社などの環境では,境界を通るSSL通信に対してDeep Inspectionの仕組みが構築されています。
まずは,SSLErrorを除去するために特定のホストをSSL検証なしで信頼するよう設定してみました。

>pip install pip-system-certs --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org
pip_system_certs: ERROR: could not register module: No module named 'wrapt'
Requirement already satisfied: pip-system-certs in c:\users\iet1\appdata\roaming\python\python312\site-packages (4.0)
Collecting wrapt>=1.10.4 (from pip-system-certs)
  Downloading wrapt-1.16.0-cp312-cp312-win_amd64.whl.metadata (6.8 kB)
Downloading wrapt-1.16.0-cp312-cp312-win_amd64.whl (37 kB)
Installing collected packages: wrapt
Successfully installed wrapt-1.16.0

これにより,wraptのエラーが解消されました。
その後は特にtrusted-hostをつけなくてもインストールできるようになりました。

他には,pip install自体に--trusted-hostをつける方法もあります。
ただ,この場合はwraptの問題が解消されていないため,最初にエラーを吐きます。
吐きはしますが,インストールは正常に完了していました。

>pip install jupyter --trusted-host pypi.org --trusted-host files.pythonhosted.org
pip_system_certs: ERROR: could not register module: No module named 'wrapt'
Collecting jupyter

個人的には`pip-system-cert`を実行すれば,以降は解決すると思っていたため,少し深堀りしてみました。

pip-system-certsのインストールによる効果について

pip-system-certsをインストールすると,pipがシステムの証明書ストアを使用するように設定されます。デフォルトでpipはPythonに付属する証明書を使うところを,OSが信頼する証明書を使うように指定していると解釈しました。

以降のインストールへの影響

pip-system-certsのインストール自体は,以降のすべてのpipコマンドでSSL検証をバイパスするわけではありません。しかし,OSが信頼する証明書を使用するようになるため,多くのSSL関連の問題が解決される可能性があります。

重要な注意点

--trusted-hostオプションは,このコマンド以降の他のpipコマンドには自動的に適用されません。
以降のインストールで同様のSSLバイパスが必要な場合は,そのたびに–trusted-hostオプションを指定する必要があります。

pip-system-certsをインストールした後は,まず通常のpipコマンド(--trusted-hostなし)でパッケージのインストールを試してみる。 それでもSSL関連の問題が発生する場合のみ,個別のインストールコマンドで--trusted-hostオプションを使用する必要がありそうです。

まとめ

結論として、このコマンドはpip-system-certsのインストール自体にSSL検証のバイパスを適用しますが,以降のすべてのインストールに自動的にSSLバイパスを適用するわけではありません。ただし,システムの証明書を使用するようになるため,多くのSSL関連の問題が解決される可能性があります。
本来はセキュリティの観点から,可能な限り標準的なSSL検証を使用することが推奨されます。

同じくエラーで悩んでいる方の参考になれば幸いです。

コメント

タイトルとURLをコピーしました