Mac 升级 openssl 后,Python可能就会出现这个错误;原因是升级openssl过程中导致库文件的链接路径发生变化

在Python 命令行输入

import ssl
Traceback (most recent call last):
   File "", line 1, in 
   File "/Users/farmer/.pyenv/versions/3.6.2/lib/python3.6/ssl.py", line 101, in 
     import _ssl             # if we can't import it, let the error propagate
 ImportError: dlopen(/Users/farmer/.pyenv/versions/3.6.2/lib/python3.6/lib-dynload/_ssl.cpython-36m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
   Referenced from: /Users/farmer/.pyenv/versions/3.6.2/lib/python3.6/lib-dynload/_ssl.cpython-36m-darwin.so
   Reason: image not found

可以发现是因为/usr/local/opt/openssl/lib/libssl.1.0.0.dylib无法加载导致的,修复这个问题只需要让Python能够正常加载到这个库,因此需要重新创建libssl.1.0.0.dylib链接。

cd /usr/local/opt/openssl/lib/


发现目录下并没有 libssl.1.0.0.dylib,只有 1.1 的库,如果已经安装了openssl1.0则直接link到这个目录,否则需要先安装openssl 1.0

我的电脑上已经安装了openssl1.0 并且也有 libssl.1.0.0.dylib库文件,只是目录在:/usr/local/opt/openssl@1.0/lib

因此只需要执行:

ln -s /usr/local/opt/openssl@1.0/lib/libssl.1.0.0.dylib /usr/local/opt/openssl/lib/libssl.1.0.0.dylib

import ssl
Traceback (most recent call last):
File “”, line 1, in
File “/Users/farmer/.pyenv/versions/3.6.2/lib/python3.6/ssl.py”, line 101, in
import _ssl # if we can’t import it, let the error propagate
ImportError: dlopen(/Users/farmer/.pyenv/versions/3.6.2/lib/python3.6/lib-dynload/_ssl.cpython-36m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib
Referenced from: /Users/farmer/.pyenv/versions/3.6.2/lib/python3.6/lib-dynload/_ssl.cpython-36m-darwin.so
Reason: image not found

重新执行import ssl 测试出现了另外一个错误,可以看出来还是相同问题,继续执行

ln -s /usr/local/opt/openssl@1.0/lib/libcrypto.1.0.0.dylib
 /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib

再次验证,解决。

打赏
Python SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”,)
Tagged on:

发表评论