无法安装Python软件包[SSL:TLSV1_ALERT_PROTOCOL_VERSION]

问题:无法安装Python软件包[SSL:TLSV1_ALERT_PROTOCOL_VERSION]

我正在尝试使用安装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

点子版本:点子9.0.1

如何解决此错误?

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

~/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

How do I fix this error?


回答 0

升级点如下:

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

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

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

本详细答案所述,这是由于最近对pip的TLS弃用。Python.org网站已停止支持 TLS版本1.0和1.1。

从Python状态页面:

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


对于PyCharm(virtualenv)用户:

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

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

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

Upgrade pip as follows:

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

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

(Note that upgrading pip using pip i.e pip install --upgrade pip will also not upgrade it correctly. It’s just a chicken-and-egg issue. pip won’t work unless using TLS >= 1.2.)

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

From the Python status page:

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


For PyCharm (virtualenv) users:

  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

但是,如果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'

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

幸运的是,无需手动升级额外的Python软件包即可解决此问题,而无需升级Python(以及整个系统),详细的逐步指南位于Stackoverflow上

注意,curlpipwget所有依赖于相同的OpenSSL lib中建立SSL连接(使用$ openssl version命令)。从curl版本7.34开始,libcurl支持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

But if the curl command 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.1 or Python version<2.7.9 (or <3.4 in Python 3) do not support the newer TLS 1.2 protocol that pip needs to connect to PyPI since about a year ago. You can easily check it in Python interpreter:

>>> 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 ssl module, compiled against old openssl lib, is lacking support for the TLSv1.2 protocol (even if the openssl library can or could be updated later).

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.

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


回答 2

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

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

最终什么工作是直接从我的浏览器下载一封来自PyPI一个较新的PIP封装(9.0.3) – https://pypi.org/simple/pip/,提取内容,然后PIP本地安装包:

pip install ./pip-9.0.3/

这解决了我的[SSL: TLSV1_ALERT_PROTOCOL_VERSION]错误。

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

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

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:

pip install ./pip-9.0.3/

This fixed my [SSL: TLSV1_ALERT_PROTOCOL_VERSION] errors.


回答 3

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

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

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

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

回答 4

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

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

如果您按照本要点所述将pip和程序包放在主目录下,则会出现此问题。

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.


回答 5

以下解决方案为我工作:

brew install python2

它还升级pip到版本1.10.1

The following solution worked for me:

brew install python2

It also upgraded pip to version 1.10.1


回答 6

检查您的TLS版本:

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

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

来源- 升级Python的时间:TLS v1.2很快将成为必需

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

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

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

编辑:您可以使用自己的专用python软件包存储库下载软件包,而不管TLS版本如何。 专用Python软件包存储库

Check your TLS version:

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.

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

You can upgrade the TLS version using the following command:

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


回答 7

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

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

This worked for me. Add sudo before python

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

回答 8

对于Python2 WIN10用户:

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

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

3.步骤2之后,您可能还会发现pip也已安装。

4.现在,如果尚未更改系统的环境,则可以立即使用pip安装软件包。“ tlsv1警报协议版本”将不会出现。

For Python2 WIN10 Users:

1.Uninstall python thoroughly ,include all folders.

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

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

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.


回答 9

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

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

安装下载pkg后对我有效

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/

works for me after installation downloaded pkg


回答 10

我也遇到了这个问题。潜在的问题是Python 2.7版本<2.7.9中的ssl库不再与pip机制兼容。

如果您在Windows上运行,并且您(像我们一样)无法轻松地从2.7的不兼容版本FWIW升级,我发现如果您从另一个最新版本的Python(例如Python 2.7)的安装中复制以下文件。 15)在另一台机器上进行安装:

    Lib\ssl.py
    libs\_ssl.lib
    DLLs\_ssl.dll

它将有效地将您的SSL层“升级”到受支持的层;这样我们就可以再次使用pip,甚至可以升级pip。

I ran into this problem as well. The underlying problem is that the ssl library in Python 2.7 versions < 2.7.9 is no longer compatible with the pip mechanism.

If you are running on Windows, and you (like us) can’t easily upgrade from an incompatible version of 2.7, FWIW, I found that if you copy the following files from another install of the latest version of Python (e.g. Python 2.7.15) on another machine to your installation:

    Lib\ssl.py
    libs\_ssl.lib
    DLLs\_ssl.dll

it will effectively “upgrade” your SSL layer to one which is supported; we were then be able to use pip again, even to upgrade pip.


回答 11

这对我有用,我安装了最新版本的pip,然后安装了库(ciscoconfparse)。

升级点数:

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

This worked for me, I installed latest version of pip and then installed the library (ciscoconfparse).

Upgrading pip:

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

回答 12

myenv:

python 2.7.14

点9.0.1

mac osx 10.9.4


mysolution:

  1. get-pip.pyhttps://packaging.python.org/tutorials/installing-packages/手动下载

  2. python get-pip.py


参考:

https://github.com/pypa/warehouse/issues/3293#issuecomment-378468534

https://packaging.python.org/tutorials/installing-packages/

安全下载get-pip.py [1]

运行python get-pip.py。[2]这将安装或升级点子。此外,如果尚未安装setuptools和wheel,则将安装它们。

确保pip,setuptools和wheel最新

尽管仅pip即可从预构建的二进制归档文件进行安装,但是最新的setuptools和wheel项目副本对于确保您也可以从源归档文件进行安装很有用:

python -m pip install --upgrade pip setuptools wheel

myenv:

python 2.7.14

pip 9.0.1

mac osx 10.9.4


mysolution:

  1. download get-pip.py manually from https://packaging.python.org/tutorials/installing-packages/

  2. run python get-pip.py


refs:

https://github.com/pypa/warehouse/issues/3293#issuecomment-378468534

https://packaging.python.org/tutorials/installing-packages/

Securely Download get-pip.py [1]

Run python get-pip.py. [2] This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.

Ensure pip, setuptools, and wheel are up to date

While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:

python -m pip install --upgrade pip setuptools wheel


回答 13

我也在Windows10上遇到了这个问题,并尝试了所有答案,但没有解决我的问题。

C:\ python367 \ Scripts> pip安装Flask

收集Flask找不到满足Flask要求的版本(来自版本:)找不到Flask的匹配发行版

之后,我发现pip配置文件已被修改。因此,我将设置pip.ini为原始默认配置,重新运行pip命令,它对我有用!

总结我的解决方案:

  1. 检查pip.ini(通常在path下C:\ProgramData\pip)是否已被修改;

  2. 如果在步骤1中为是,请尝试将其重置为默认配置。

I also hit this problem on my windows10 and tried all the answers but didn’t solve my problem.

C:\python367\Scripts>pip install Flask

Collecting Flask Could not find a version that satisfies the requirement Flask (from versions: ) No matching distribution found for Flask

After that, I find the pip configuration file had been modified. So, I set the pip.ini as the original default configuration, re-run the pip command and it works for me!

In summary of the solution of mine:

  1. Check the pip.ini (usually under the path C:\ProgramData\pip) had been modified;

  2. If yes in step1, try to reset it to a default configuration.


回答 14

或者只是所需的库不在存储库中。我是Python新手,关于升级pip的所有建议最终都被误导了。我只需要查看https://pypi.org/,发现该库(在我的情况下为气流)停在某个旧版本上,然后将其重命名。是的,愚蠢的解决方案也是可能的:-)。

Or simply the required library just isn’t in the repo. I’m Python newbie and all advices about upgrading pip finally shown as misleading. I had just to look into https://pypi.org/ , finding the library (airflow in my case) stopped at some old version, after which it was renamed. Yes, also that silly solution is also possible :-).


回答 15

对于所有python3pip3用户:

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

然后假设您要安装熊猫

pip3 install pandas --user

For all the python3 and pip3 users out there:

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

and then assume you want to install pandas

pip3 install pandas --user

回答 16

通过以下方式安装pip的答案:

  1. curl https://bootstrap.pypa.io/get-pip.py |sudo python 要么
  2. curl https://bootstrap.pypa.io/get-pip.py | python

由于我不断收到错误,因此对我不起作用:

Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
ERROR: Could not find a version that satisfies the requirement pip (from versions: none)
ERROR: No matching distribution found for pip

我必须pip通过以下方式手动安装:

  1. 前往pip发行网站
  2. 下载tar.gz版本
  3. 将文件解压缩到本地并cd进入目录
  4. python setup.py install

The answers of installing pip via:

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

did not work for me as I kept on getting the error:

Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
ERROR: Could not find a version that satisfies the requirement pip (from versions: none)
ERROR: No matching distribution found for pip

I had to install pip manually via:

  1. Go the pip distribution website
  2. Download the tar.gz version
  3. Unpack the file locally and cd into the directory
  4. run python setup.py install