継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

pip install した時の SSL エラーが発生した場合の対応を整理した

タダです.

Mac のローカルで pip installしようとした時に SSL 関連のエラーの事象にあたりました.突然,pip installの実行ができなくなったため,この記事で対応方法を整理します.

事象の概要

boto3 を導入しようとしてみたところ,pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.と表示され,SSL のモジュールで利用できるものがないと表示されてコケました.

» pip install boto3                       
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
~中略~
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

実行環境は次の通りです.

  • macOS Majave
  • Python 3.6.8
    • pyenv で導入したもの

結論

結論としては, Python と pip のバージョンを上げて対応しました.

エラー対応の時系列

OpenSSL 周りを再インストール

まず,SSL 関連のエラーがでたのでbrew reinstall opensslを実行して OpenSSL を入れ直しました.

» brew unlink openssl        
Unlinking /usr/local/Cellar/openssl@1.1/1.1.1f... 0 symlinks removed
» brew reinstall openssl
==> Reinstalling openssl@1.1 
==> Downloading https://homebrew.bintray.com/bottles/openssl@1.1-1.1.1f.mojave.bottle.tar.gz
Already downloaded: /XXX/XXX/Library/Caches/Homebrew/downloads/f46ab457fe1e0a3e8679dd2725494506e01504b448ace015cf5d5eee0c7307f0--openssl@1.1-1.1.1f.mojave.bottle.tar.gz
==> Pouring openssl@1.1-1.1.1f.mojave.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because openssl/libressl is provided by macOS so don't link an incompatible version.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1f: 8,057 files, 18MB

再度,pip installしてみるも事象改善しないため次のアプローチを検討しました.

pip install boto3                                   
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
~中略~
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
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

Python と pip のバージョンアップ

下記の issue を参考に Python が 3.6.8 だったところを 3.8 系に上げて,pip もバージョンアップしてみました.

github.com

» pyenv install --list
Available versions:
~中略~
3.8.0
~中略~
» pyenv install 3.8.0
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.0.tar.xz...
-> https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz
Installing Python-3.8.0...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.0 to /XXX/XXX/.pyenv/versions/3.8.0
» pyenv global 3.8.0
» curl -kL https://bootstrap.pypa.io/get-pip.py | python
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1764k  100 1764k    0     0  1956k      0 --:--:-- --:--:-- --:--:-- 1954k
Collecting pip
  Downloading pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
     |████████████████████████████████| 1.4 MB 3.0 MB/s
Collecting wheel
  Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Installing collected packages: pip, wheel
  Attempting uninstall: pip
    Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2 wheel-0.34.2

再度,pip install してみたらコケずに boto3 を導入できました.

» pip install boto3
Collecting boto3
  Downloading boto3-1.12.36-py2.py3-none-any.whl (128 kB)
     |████████████████████████████████| 128 kB 1.3 MB/s
Collecting s3transfer<0.4.0,>=0.3.0
  Using cached s3transfer-0.3.3-py2.py3-none-any.whl (69 kB)
Collecting jmespath<1.0.0,>=0.7.1
  Using cached jmespath-0.9.5-py2.py3-none-any.whl (24 kB)
Collecting botocore<1.16.0,>=1.15.36
  Downloading botocore-1.15.36-py2.py3-none-any.whl (6.1 MB)
     |████████████████████████████████| 6.1 MB 3.3 MB/s
Collecting urllib3<1.26,>=1.20; python_version != "3.4"
  Using cached urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
Collecting docutils<0.16,>=0.10
  Using cached docutils-0.15.2-py3-none-any.whl (547 kB)
Collecting python-dateutil<3.0.0,>=2.1
  Using cached python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
Collecting six>=1.5
  Using cached six-1.14.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: urllib3, docutils, jmespath, six, python-dateutil, botocore, s3transfer, boto3
Successfully installed boto3-1.12.36 botocore-1.15.36 docutils-0.15.2 jmespath-0.9.5 python-dateutil-2.8.1 s3transfer-0.3.3 six-1.14.0 urllib3-1.25.8

まとめ

これまで動作していた pip installSSL エラーが発生したのでその対処をまとめました.突然のpip installが動作しなくなる事象は戸惑ったので同じ事象に遭遇したら,Python や pip のバージョンを見直してみるのは1つの手段してみると良いかもしれません.