无法安装Python包[SSL:TLSV1_ALERT_PROTOCOL_VERSION]

2024-06-11 18:27:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用pip安装一个Python库,得到一个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版本:pip 9.0.1

如何修复此错误?


Tags: installpipthemastersslbaseversion错误
3条回答

在OS X上执行@Anupam的回答后,无论我使用何种权限运行它,都会导致以下错误:

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

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

pip install ./pip-9.0.3/

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

但是,如果curl命令本身由于错误而失败,或者即使在升级pip之后仍然存在“tlsv1警报协议版本”,则意味着您的操作系统的底层OpenSSL库版本<;1.0.1或Python版本<;2.7.9(或Python 3中的<;3.4)不支持pip所需的较新TLS 1.2协议从about a year ago开始连接到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'

AttributeError(而不是预期的“5”)意味着您的Python stdlibssl模块(根据旧的openssl库编译)缺少对TLSv1.2协议的支持(即使openssl库可以或可以在以后更新)。

幸运的是,不升级Python(以及整个系统)就可以通过手动安装额外的Python包来解决这个问题——详细的分步指南是available here on Stackoverflow

Note, curl and pip and wget all depend on the same OpenSSL lib for establishing SSL connections (use $ openssl version command). libcurl supports TLS 1.2 since curl version 7.34, but older curl versions should be able to connect if you had OpenSSL version 1.0.2 (or later).


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

升级pip如下:

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

注意:如果不是在虚拟环境中,则可能需要使用上面的sudo python

(注意,使用pippip install --upgrade pip升级pip也不会正确升级。这只是鸡和蛋的问题。pip除非使用TLS>;=1.2,否则无法工作

this detailed answer中所述,这是由于最近对pip使用了TLS。Python.org站点有用于TLS版本1.0和1.1的stopped support

从Python状态页:

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


对于PyCharm(virtualenv)用户:

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

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

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

相关问题 更多 >