在 macOS 10.14.6 上的 Python 3.7.4 中导入 ssl 时出错

问题描述 投票:0回答:3

为什么我在尝试在 python3 中导入 ssl 时出现错误?

Python 3.7.4 (default, Jul  9 2019, 18:13:23) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ImportError: dlopen(/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ssl.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
  Referenced from: /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ssl.cpython-37m-darwin.so
  Reason: image not found

我很难通过 pip 安装软件包,这似乎引发了 SSL 错误。

python-3.x ssl
3个回答
13
投票

我遇到了完全相同的问题,当我使用brew将Python版本升级到3.7.6后,这个问题得到了解决。

brew upgrade python3

这引发了权限被拒绝的错误,但更新仍然完成,之后我能够导入 ssl:

Python 3.7.6 (default, Dec 30 2019, 19:38:28) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> ssl
<module 'ssl' from '/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py'>
>>> 

8
投票

在 osx 上使用 homebrew 解决这个问题的方法是切换到旧版本的 openssl:

brew switch openssl 1.0.2s

参见这篇SO文章


0
投票

对于 3.9+ Python 版本遇到此问题的任何人,我发现重新安装 Python 成功。

使用

pyenv
(替换您的版本):

  1. pyenv uninstall 3.11
  2. pyenv install 3.11

检查您的安装是否有效:

  1. pyenv local 3.11
  2. python
  3. import ssl
    (Python shell 内部)
  4. 不应出现错误
  5. exit()
    返回命令行
© www.soinside.com 2019 - 2024. All rights reserved.