【Python】pipenv/pipからrequestsモジュールがインストールできない(the ssl module in Python is not available)

python-logo Python

現象

pipenv install requestsを行ったところ、
下記のようなエラーが出力された。

[pipenv.exceptions.ResolutionFailure]:   File "/Users/username/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pipenv/utils.py", line 395, in resolve
[pipenv.exceptions.ResolutionFailure]:       raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]:       pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches requests
[pipenv.exceptions.ResolutionFailure]:       No versions found
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches requests
No versions found
Was https://pypi.org/simple reachable?

ちなみに、pipを使ってインストールしようとした場合も
下記のようなエラーが出力されてしまった。

pip install requests
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting requests
  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/requests/
  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/requests/
  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/requests/
  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/requests/
  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/requests/
  Could not fetch URL https://pypi.org/simple/requests/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/requests/ (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 requests (from versions: )
No matching distribution found for requests
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

エラー文言を見ると
「SSLモジュールが有効ではないため、HTTPS接続できません」
と言われている。

この時利用していたバージョン

$ python --version
Python 3.7.3

$ pip -V
pip 19.0.3 from /Users/username/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)

pipでインストールする際の解決策

pipでインストールすることに関しては、
pythonのバージョンを上げることで解決できた。

私はpyenvでディレクトリごとPythonのバージョンを管理しているので、
pyenvによるバージョン指定を行う。

$ pyenv local 3.8.5
$ python --version
Python 3.8.5

$ pip install requests
Collecting requests
  Using cached requests-2.24.0-py2.py3-none-any.whl (61 kB)
Requirement already satisfied: certifi>=2017.4.17 in /Users/username/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from requests) (2020.6.20)
Requirement already satisfied: chardet<4,>=3.0.2 in /Users/username/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from requests) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /Users/username/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from requests) (2.10)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/username/.pyenv/versions/3.8.5/lib/python3.8/site-packages (from requests) (1.25.10)
Installing collected packages: requests
Successfully installed requests-2.24.0
WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.
You should consider upgrading via the '/Users/username/.pyenv/versions/3.8.5/bin/python3.8 -m pip install --upgrade pip' command.
$

しかし、これだけではpipenvによるインストール時の問題までは
解決しなかった。

$ pipenv --rm
$ pipenv lock --clear
$ pipenv install
$ pipenv install requests
Installing requests…
Error:  An error occurred while installing requests!
Error text: Could not fetch URL https://pypi.org/simple/requests/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/requests/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: 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/requests/
WARNING: 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/requests/
WARNING: 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/requests/
WARNING: 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/requests/
WARNING: 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/requests/
ERROR: Could not find a version that satisfies the requirement requests (from -r /var/folders/6r/q2cbhnr134sbxq1f1_405r5r0000gn/T/pipenv-dzx_5e5v-requirements/pipenv-1wj7o9c1-requirement.txt (line 1)) (from versions: none)
ERROR: No matching distribution found for requests (from -r /var/folders/6r/q2cbhnr134sbxq1f1_405r5r0000gn/T/pipenv-dzx_5e5v-requirements/pipenv-1wj7o9c1-requirement.txt (line 1))

✘ Installation Failed

まだSSL証明書で怒られている。

pipenvでインストールする際の解決策

これの原因は、Pipfileに記載されているPythonのバージョンが
3.7に固定されていることでした。

変更前Pipfile

[source]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.7"

バージョン3.7が指定されている状態だと、
--pythonオプションによるプロジェクトの初期化でも
下記のようなwarningが出現する。

$ pipenv --python 3.8
# 中略
Warning: Your Pipfile requires python_version 3.7, but you are using 3.8.5 (/Users/username/.local/share/v/a/bin/python).
  $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
  $ pipenv check will surely fail.

そのため、Pipfileで記載されいてるPythonのバージョンを
直接編集し、再度初期化を行った。(3.73.8

変更後Pipfile

[source]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.8"
$ pipenv --rm
$ pipenv lock --clear
$ pipenv --python 3.8
# warningが出なくなった

$ pipenv install requests
Installing requests…
Adding requests to Pipfile's [packages]…
✔ Installation Succeeded
Pipfile.lock (db4242) out of date, updating to (fbd99e)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (fbd99e)!
Installing dependencies from Pipfile.lock (fbd99e)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

まとめ

  • Pythonを3.8にバージョンアップする
  • pippipenvをそれに合わせたバージョンにする(pipは自動的にPythonのバージョンにひきづられる)
  • Pipfileのバージョンも3.8にしておく
タイトルとURLをコピーしました