LoginSignup
0
0

PythonがOpenSSL周りのエラーで動かない

Posted at

はじめに

こんにちは、ユーゴです。今回は、Pythonをローカルで使用していた時に起きたエラーについて解説します。「バイトでPythonを使っているのに、環境が壊れているのはヤバい!」となり、慌てて直しました。
ちなみにdockerでは、さすが仮想環境なだけあり無事に実行できました。

環境

・macOS Sonoma:14.4.1
・Python:3.16.2
・pip:24.0

課題

「jupyter notebook」や「pip install」を実行するとき、また「reqeusts」ライブラリを使う処理を実行するときなど、OpenSSL周りのエラーが起きる。

例えば、jupyter notebookでは以下のようなエラーが出た。

Traceback (most recent call last):
  File "/Users/xxxxx/.pyenv/versions/3.11.6/bin/jupyter-notebook", line 5, in <module>
    from notebook.app import main
  File "/Users/xxxxx/.pyenv/versions/3.11.6/lib/python3.11/site-packages/notebook/app.py", line 12, in <module>
    from jupyter_server.base.handlers import JupyterHandler
  File "/Users/xxxxx/.pyenv/versions/3.11.6/lib/python3.11/site-packages/jupyter_server/base/handlers.py", line 21, in <module>
    import prometheus_client
  File "/Users/xxxxx/.pyenv/versions/3.11.6/lib/python3.11/site-packages/prometheus_client/__init__.py", line 3, in <module>
    from . import (
  File "/Users/xxxxx/.pyenv/versions/3.11.6/lib/python3.11/site-packages/prometheus_client/exposition.py", line 8, in <module>
    import ssl
  File "/Users/xxxxx/.pyenv/versions/3.11.6/lib/python3.11/ssl.py", line 100, in <module>
    import _ssl             # if we can't import it, let the error propagate
    ^^^^^^^^^^^
ImportError: dlopen(/Users/xxxxx/.pyenv/versions/3.11.6/lib/python3.11/lib-dynload/_ssl.cpython-311-darwin.so, 0x0002): Library not loaded: /usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib
  Referenced from: <DD150696-CAB5-3AB7-906C-3ADC45D3C658> /Users/xxxxx/.pyenv/versions/3.11.6/lib/python3.11/lib-dynload/_ssl.cpython-311-darwin.so
  Reason: tried: '/usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib' (no such file), '/usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib' (no such file)

原因

おそらく、何かの拍子にOpenSSLの環境を破壊しています。自分が破壊した原因は不明です。

解決策

OpenSSLを入れ直し、Pythonも入れ直します。
以降、ターミナルで実行します。

1. OpenSSLをインストール

brew install openssl@1.1

2. 環境変数を設定

export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

3. Pythonを入れ直す

pyenv uninstall 3.11.6
pyenv install 3.11.6

4. 確認

無事動きました。

まとめ

いかがだったでしょうか。今回は、Pythonで起きたOpenSSL周りのエラーについて解説しました。
このように、Pythonの技術的な紹介から、Unity, AWSなどの技術的な紹介もしています。お役に立てましたら、いいね, LGTM, フォローなどよろしくお願いします!

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