无法安装 Python 包 [SSL: TLSV1_ALERT_PROTOCOL_VERSION]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49768770/
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 19:14:40  来源:igfitidea点击:

Not able to install Python packages [SSL: TLSV1_ALERT_PROTOCOL_VERSION]

pythonpython-2.7pipssl-certificate

提问by Nishant Nawarkhede

I am trying to install a Python library using pip, getting an SSL error:

我正在尝试使用 安装 Python 库pip,但出现 SSL 错误:

~/projects/base  pre-master±  pip install xdict

Collecting xdict
  Could not fetch URL https://pypi.python.org/simple/xdict/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) - skipping
  Could not find a version that satisfies the requirement xdict (from versions: )
No matching distribution found for xdict

pip version: pip 9.0.1

pip 版本:pip 9.0.1

How do I fix this error?

我该如何解决这个错误?

回答by Anupam

Upgrade pip as follows:

升级pip如下:

curl https://bootstrap.pypa.io/get-pip.py | python

Note: You may need to use sudo pythonabove if not in a virtual environment.

注意:sudo python如果不是在虚拟环境中,您可能需要使用上述内容。

(Note that upgrading pipusing pipi.e pip install --upgrade pipwill also not upgrade it correctly. It's just a chicken-and-egg issue. pipwon't work unless using TLS >= 1.2.)

(请注意,pip使用pipiepip install --upgrade pip升级也不会正确升级。这只是一个先有鸡还是先有蛋的问题。pip除非使用 TLS >= 1.2,否则将无法工作。)

As mentioned in this detailed answer, this is due to the recent TLS deprecation for pip. Python.org sites have stopped supportfor TLS versions 1.0 and 1.1.

如此详细答案中所述,这是由于最近 TLS 弃用了 pip。Python.org 站点已停止支持TLS 1.0 和 1.1 版。

From the Python status page:

从 Python 状态页面:

Completed- The rolling brownouts are finished, and TLSv1.0 and TLSv1.1 have been disabled. Apr 11, 15:37 UTC

已完成- 滚动停电已完成,并且已禁用 TLSv1.0 和 TLSv1.1。世界标准时间 4 月 11 日 15:37


For PyCharm (virtualenv) users:


对于 PyCharm (virtualenv) 用户:

  1. Run virtual environment with shell. (replace "./venv/bin/activate" to your own path)

    source ./venv/bin/activate
    
  2. Run upgrade

    curl https://bootstrap.pypa.io/get-pip.py | python
    
  3. Restart your PyCharm instance, and check your Python interpreter in Preference.

  1. 使用 shell 运行虚拟环境。(将“./venv/bin/activate”替换为您自己的路径)

    source ./venv/bin/activate
    
  2. 运行升级

    curl https://bootstrap.pypa.io/get-pip.py | python
    
  3. 重新启动您的 PyCharm 实例,并在Preference 中检查您的 Python 解释器。

回答by Alex C.

But if the curlcommand itself fails with error, or "tlsv1 alert protocol version"persists even after upgrading pip, it means your operating system's underlying OpenSSL library version<1.0.1or Python version<2.7.9(or <3.4in Python 3) do not support the newer TLS 1.2 protocol that pipneeds to connect to PyPI since about a year ago. You can easily check it in Python interpreter:

但是,如果curl命令本身失败并出现错误,或者“tlsv1 警报协议版本”即使在升级后仍然存在pip,则意味着您操作系统的底层 OpenSSL 库版本<1.0.1或 Python 版本< 2.7.9(或3.4Python 3 中的< )不支持较新的 TLS 1.2 协议这pip需要连接到PyPI中,因为大约一年前。您可以在 Python 解释器中轻松检查它:

>>> import ssl
>>> ssl.OPENSSL_VERSION
'OpenSSL 0.9.8o 01 Jun 2010'
>>> ssl.PROTOCOL_TLSv1_2
 AttributeError: 'module' object has no attribute 'PROTOCOL_TLSv1_2'

The AttributeError(instead of expected '5') means your Python stdlib sslmodule, compiled against old openssl lib, is lacking support for the TLSv1.2 protocol (even if the openssl library can or could be updated later).

AttributeError(而不是预期的“5”)是指你的Python STDLIBssl模块,对旧的OpenSSL的lib编译,缺乏对TLSv1.2工作协议(即使OpenSSL库可以或可以在以后更新)的支持。

Fortunately, it can be solved without upgrading Python(and the whole system), by manually installing extra Python packages -- the detailed step-by-step guide is available here on Stackoverflow.

幸运的是,它可以在不升级 Python(和整个系统)的情况下解决,通过手动安装额外的 Python 包 - Stackoverflow 上提供了详细的分步指南。

Note, curland pipand wgetall depend on the same OpenSSL lib for establishing SSL connections (use $ openssl versioncommand). libcurl supports TLS 1.2 since curl version 7.34, but older curl versions shouldbe able to connect ifyou had OpenSSL version 1.0.2 (or later).


P.S.
For Python 3, please use python3and pip3everywhere (unless you are in a venv/virtualenv), including the curlcommand from above:
$ curl https://bootstrap.pypa.io/get-pip.py | python3 --user

注意,curlpipwget所有依赖于相同的OpenSSL lib中建立SSL连接(使用$ openssl version命令)。libcurl 从 curl 版本7.34开始支持 TLS 1.2 ,但如果您有 OpenSSL 版本 1.0.2(或更高版本),旧的 curl 版本应该能够连接。


PS
对于Python 3,请在任何地方使用python3pip3(除非您在 venv/virtualenv 中),包括上面curl命令:
$ curl https://bootstrap.pypa.io/get-pip.py | python3 --user

回答by T Dub

Following @Anupam's answer on OS X resulted in the following error for me, regardless of permissions I ran it with:

遵循@Anupam 在 OS X 上的回答导致我出现以下错误,无论我运行它的权限如何:

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ...

由于环境错误,无法安装软件包:[Errno 13] 权限被拒绝:...

What eventually worked was to download a newer pip package (9.0.3) from PyPI directly from my browser - https://pypi.org/simple/pip/, extract the contents, and then pip install the package locally:

最终有效的是直接从我的浏览器 - https://pypi.org/simple/pip/从 PyPI 下载更新的 pip 包(9.0.3),提取内容,然后在本地 pip 安装包:

pip install ./pip-9.0.3/

This fixed my [SSL: TLSV1_ALERT_PROTOCOL_VERSION]errors.

这修复了我的[SSL: TLSV1_ALERT_PROTOCOL_VERSION]错误。

回答by Ilya Gazman

@Anupam's solution worked for me. However, I had to use sudoand specify the exact location of my virtual Python environment:

@Anupam的解决方案对我有用。但是,我必须使用sudo并指定我的虚拟 Python 环境的确切位置:

curl https://bootstrap.pypa.io/get-pip.py | sudo /Users/{your user name}/{path to python}/bin/python

回答by Hbar

To upgrade the local version I used a slight variant:

为了升级本地版本,我使用了一个轻微的变体:

curl https://bootstrap.pypa.io/get-pip.py | python - --user

This problem arises if you keep your pip and packages under your home directory as described in this gist.

如果您按照本要点中的描述将 pip 和包保存在主目录下,则会出现此问题。

回答by Monish Lalchandani

The following solution worked for me:

以下解决方案对我有用:

brew install python2

It also upgraded pipto version 1.10.1

它也升级pip到了 1.10.1 版本

回答by Vithulan

Check your TLS version:

检查您的 TLS 版本:

python2 -c "import urllib2,json; print(json.loads(urllib2.urlopen('https://www.howsmyssl.com/a/check').read())['tls_version'])"

If your TLS version is less than 1.2 you have to upgrade it since the PyPI repository is on a brownout period of deprecating early TLS.

如果您的 TLS 版本低于 1.2,您必须升级它,因为 PyPI 存储库正处于弃用早期 TLS 的限制期。

Source - Time To Upgrade Your Python: TLS v1.2 Will Soon Be Mandatory

来源 - 是时候升级您的 Python:TLS v1.2 将很快成为强制性要求

You can upgrade the TLS version using the following command:

您可以使用以下命令升级 TLS 版本:

sudo apt-get update && sudo apt-get install openssl libssl-dev

This should fix your problem. Good luck!

这应该可以解决您的问题。祝你好运!

EDIT: You can download packages using your own private python package repository regardless of TLS version. Private Python Package Repository

编辑:无论 TLS 版本如何,您都可以使用自己的私有 python 包存储库下载包。 私有 Python 包存储库

回答by Optimus Prime

For Python2 WIN10 Users:

对于 Python2 WIN10 用户:

1.Uninstall python thoroughly ,include all folders.

1.彻底卸载python,包括所有文件夹。

2.Fetch and install the lastest python-2.7.msi (ver 2.7.15)

2.获取并安装最新的python-2.7.msi (ver 2.7.15)

3.After step 2,you may find pip had been installed too.

3.在第2步之后,你可能会发现pip也已经安装好了。

4.Now ,if your system'env haven't been changed,you can use pip to install packages now.The "tlsv1 alert protocol version" will not appear.

4.现在,如果你的系统环境没有改变,你现在可以使用pip安装包。“tlsv1警报协议版本”不会出现。

回答by Sugoi Reed

This worked for me. Add sudo before python

这对我有用。在python之前添加sudo

curl https://bootstrap.pypa.io/get-pip.py |sudo python

回答by keypoint

I tried all existing fixes and not working for me

我尝试了所有现有的修复程序,但对我不起作用

I re-install python 2.7 (will also install pip) by downloading .pkg at https://www.python.org/downloads/mac-osx/

我通过在https://www.python.org/downloads/mac-osx/下载 .pkg 重新安装 python 2.7(也将安装 pip)

works for me after installation downloaded pkg

安装下载 pkg 后对我有用