0
0

More than 1 year has passed since last update.

[Macでkeras]sslでインポートできない時

Last updated at Posted at 2021-12-30

問題のコードと結果

コード

from tensorflow.keras.datasets import mnist

結果

Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)

原因

macOSに標準でインストールされているOpenSSLが古すぎるため、Python 3.6以降ではmacOS用のインストーラーにはOpenSSLが同梱され、システムのOpenSSLは参照されなくなりました。

これによって、OSにインストールされたルート証明書も参照されず、インストール直後の状態ではルート証明書が含まれていません1。このため、TLSサーバー証明書の検証に失敗します。

引用: macOS用公式インストーラーのPython 3.6でCERTIFICATE_VERIFY_FAILEDとなる問題

対策

  1. opensslをインストール
brew install openssl
  1. ルート証明書を参照可能にする

certifiモジュールをダウンロード

pip3 install certifi

pythonのバージョンを確認する。

python3 --version
## 私は3.8でした

ルート証明書を参照可能にする
pythonのバージョンを以下のコマンド中に記入して実行

/Applications/Python\ 3.8/Install\ Certificates.command
ls -l /Library/Frameworks/Python.framework/Versions/3.8/etc/openssl/

Permission deniedでopensslがインストールできないとき

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0