Python 中的 SSL 模块不可用(在 OSX 上)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/58280484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:41:01  来源:igfitidea点击:

SSL module in Python is not available (on OSX)

pythonmacosssl

提问by John M.

I'm having trouble running pip installin a virtualenv on OSX 10.13. I already have run brew install openssland the path /usr/local/include/opensslpoints to ../opt/openssl/include/openssl. Does anyone know how to fix this? This started happening after I reinstalled pythonusing brew install.

pip install在 OSX 10.13 上的 virtualenv 中运行时遇到问题。我已经运行brew install openssl并且路径/usr/local/include/openssl指向../opt/openssl/include/openssl. 有谁知道如何解决这一问题?在我python使用brew install.

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Collecting Pillow Could not fetch URL https://pypi.python.org/simple/pillow/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping Could not find a version that satisfies the requirement Pillow (from versions: ) No matching distribution found for Pillow

pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用。收集枕头无法获取 URL https://pypi.python.org/simple/pillow/:确认 ssl 证书时出现问题:无法连接到 HTTPS URL,因为 SSL 模块不可用。- 跳过 找不到满足枕头要求的版本(来自版本:)找不到枕头的匹配发行版

UPDATE: Here's some more info:

更新:这里有更多信息:

? which python
/usr/local/opt/python/libexec/bin/python
? which pip
/usr/local/opt/python/libexec/bin/pip
? python --version
Python 3.7.4
? pip --version
pip 19.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
? brew info python
python: stable 3.7.4 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python/3.6.5_1 (4,795 files, 100.0MB)
  Poured from bottle on 2019-10-08 at 14:39:37
/usr/local/Cellar/python/3.7.4_1 (3,903 files, 60.6MB) *
  Poured from bottle on 2019-10-08 at 14:37:10
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/python.rb

Yes, I have both 3.6.5_1 and 3.7.4_1 installed as I may need to switch between the two at times.

是的,我同时安装了 3.6.5_1 和 3.7.4_1,因为我有时可能需要在两者之间切换。

? brew unlink openssl
Unlinking /usr/local/Cellar/openssl/1.0.2s... 0 symlinks removed

回答by ivan_pozdeev

The sslmodule as well as its underlying C extension appears to be a part of the pythonformula:

ssl模块及其底层 C 扩展似乎是python公式的一部分:

Mac-Admin:~ admin$ python3
Python 3.7.4 (default, Sep  7 2019, 18:27:02) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> ssl
<module 'ssl' from '/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py'>
>>> import _ssl
>>> _ssl
<module '_ssl' from '/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ssl.cpython-37m-darwin.so'>

so it being missing most probably means package installation corruption which brew reinstall pythonshould fix.

所以它丢失很可能意味着brew reinstall python应该修复的包安装损坏。



Also note that while Homebrew allows multiple versions to coexist, its installation logic isn't quite designed to keep the alternative versions operationalunless they are installed via a versioned formula (and e.g. routinely removes old versions in the regular brew cleanup).

还要注意的是,虽然 Homebrew 允许多个版本共存,但它的安装逻辑并不是为了保持替代版本的运行而设计的,除非它们是通过版本化公式安装的(例如,定期删除常规 中的旧版本brew cleanup)。

So consider using pyenv(also available via brew) if you need to routinely switch between Python versions -- or some 3rd-party tap that offers versioned formulae for it.

因此,如果您需要定期在 Python 版本之间切换,pyenv请考虑使用(也可通过brew) - 或某些为其提供版本化公式的 3rd-party tap。

回答by Matt Morgan

Mac OSX Catalina (and same issue on OSX Mojave) Pyenv

Mac OSX Catalina(以及 OSX Mojave 上的相同问题) Pyenv

For anyone searching this topic, I had the same presenting problem, but had Python installed via both Homebrew and Pyenv!! It would have been better (IMO) to just use Pyenv to easily manage versions. As mentioned by @ivan_pozdeev in their answer, but here's some detail you might want.

对于搜索此主题的任何人,我都遇到了同样的问题,但是通过 Homebrew 和 Pyenv 安装了 Python!仅使用 Pyenv 来轻松管理版本会更好(IMO)。正如@ivan_pozdeev 在他们的回答中提到的,这里有一些你可能想要的细节。

If your situation is similar, none of the above solutions would be quite enough to set things right. Partially I was helped by a Pyenv related answer here: https://stackoverflow.com/a/51797298/3084820I also happened to have pyenv-virtualenvinstalled, so mentioning that as well, as it's common to use these two together.

如果您的情况类似,上述解决方案都不足以解决问题。部分地,我在这里得到了 Pyenv 相关答案的帮助:https://stackoverflow.com/a/51797298/3084820我也碰巧pyenv-virtualenv安装了,所以也提到这一点,因为将这两个一起使用是很常见的。

I finally took the following steps to resolve the issue:

我最终采取了以下步骤来解决问题:

brew uninstall python
rm -rf $(pyenv root)
brew uninstall pyenv-virtualenv   # you may not have this installed, but...
brew uninstall pyenv

Now, for a clean installation manageable with Pyenv:

现在,要使用 Pyenv 进行干净的安装:

brew install pyenv
pyenv install 3.6.10  (or whatever version you want)

This gave me a clean, working install of Python 3.6.10, and if I wanted or needed to, I could install a different version and switch between with Pyenv.

这给了我一个干净、有效的 Python 3.6.10 安装,如果我想要或需要,我可以安装不同的版本并在 Pyenv 之间切换。

回答by charlton_austin

I had a similar problem with Catalina and could not get homebrew reinstall to work. I tried several thing.

我在 Catalina 上遇到了类似的问题,无法重新安装自制软件。我尝试了几件事。

brew reinstall openssl
brew reinstall pyenv
brew reinstall pyenv-virtualenv

Ultimately the only thing that worked for me was to completely uninstall both as well as the underlying python installations and then reinstall everything.

最终,唯一对我有用的是完全卸载两者以及底层的 python 安装,然后重新安装所有东西。

brew uninstall pyenv pyenv-virtualenv
brew install pyenv pyenv-virtualenv
pyenv uninstall 3.x.x
pyenv install 3.x.x
pip install -r requirements.txt

回答by tzatalin

My fix is to reinstall pyenv and python

我的解决方法是重新安装 pyenv 和 python

 brew uninstall pyenv pyenv-virtualenv
 brew install pyenv pyenv-virtualenv
 pyenv uninstall 3.6.5
 pyenv install 3.6.5

回答by baskcat

I also had this error and I fixed it with brew update && brew upgrade

我也有这个错误,我用 brew update && brew upgrade

回答by Yasunari Nishi

I had the same error and it was because I was using python 3.6.5 in my pyenv environment. The below treatment worked for me.

我遇到了同样的错误,这是因为我在 pyenv 环境中使用了 python 3.6.5。以下治疗对我有用。

pyenv install 3.7.3
pyenv global 3.7.3

回答by Bennie Lamb

I have the same issue, but reinstall hasn't worked for me.

我有同样的问题,但重新安装对我不起作用。

> which python3     
/usr/local/bin/python3
blamb@cha-109164-l:/usr/local/bin|master

> which pip3
/usr/local/bin/pip3
blamb@cha-109164-l:/usr/local/bin|master

> pip3 -V
pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
blamb@cha-109164-l:/usr/local/bin|master 

> python3 -V
Python 3.7.0
blamb@cha-109164-l:/usr/l