尝试使用Python 3.7.2 pip安装软件包会导致TSL/SSL错误 [英] Trying to install packages with Python 3.7.2 pip causes TSL/SSL errors

查看:89
本文介绍了尝试使用Python 3.7.2 pip安装软件包会导致TSL/SSL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跑步:

Amazon Linux AMI 2018.03.0

Linux ip-xxx-yy-z-ww 4.14.77-70.59.amzn1.x86_64 #1 SMP Mon Nov 12 22:02:45 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

多python环境.它同时安装了Python 2.7.15和3.7.2.

Multi-python environment. It has both Python 2.7.15 and 3.7.2 installed.

常规(Python2)pip正常运行.

Regular (Python2) pip works without a hitch.

当我尝试运行pip3 install flask时,出现以下错误:

When I try to run pip3 install flask I get the following errors:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting flask
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
  Could not fetch URL https://pypi.org/simple/flask/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/flask/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
  Could not find a version that satisfies the requirement flask (from versions: )
No matching distribution found for flask
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
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

如果我尝试安装其他任何软件包,也会出现相同的错误.

The same error appears if I try to install any other package.

pip3版本为18.1

pip3 version is 18.1

添加--trusted-host pypi.org无济于事.

安装ca证书没有帮助

根据以下指南安装了Python 3: https://stackoverflow.com/a/8112006/8826349

Python 3 installed according to the following guide: https://stackoverflow.com/a/8112006/8826349

我现在注意到,在Python 3上运行make时,它成功完成,但是显示以下消息:

I now noticed, that when running make on Python 3, it finished successfully, but had the following message:

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_hashlib              _ssl                  _tkinter
_uuid
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  atexit                pwd
time


Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

我的问题似乎与相同.但是,该线程除了描述问题外没有提供任何解决方案.

My issue seems to be identical to this. However the thread does not provide any solutions beyond describing the problem.

我尝试使用指南.但是较新的openssl版本无法解决我的问题.

I've tried to compile a newer version of openssl using this guide. But newer openssl version did not resolve my issue.

我无法找到此问题的解决方案.

I was unable to find a solution for this issue.

Amazon的Linux版本似乎无法在Python 3上正常运行.

It seems that Amazon's Linux version does not work well with Python 3.

他们确实有一个不同的操作系统,称为Amazon Linux 2,在其中安装Python 3就像运行yum install python3

They do have a different OS, called Amazon Linux 2, where installiing Python 3 is as easy as running yum install python3

但是,Amazon Linux 2有其自身的问题.它的Python 2安装缺少pip.您可以通过运行以下命令手动安装它:

However, Amazon Linux 2 has it's own issues. It's Python 2 installation lacks pip. You can install it manually by running:

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

但这实际上覆盖了pip3,因此Python 3仍然没有pip.

But this actually overrides pip3, so then Python 3 remains without a pip.

推荐答案

因此,SSLError(由于SSL模块不可用,因此无法连接到HTTPS URL.")在此上下文中表示编译python3.7时从来源来看,它没有与您的系统OpenSSL库链接.完善的解决方案是确保您除了lib之外还具有所需的标头,例如:
$ yum install openssl-devel.x86_64 openssl.x86_64
并从头开始重新编译,并链接到OpenSSL库.

So, SSLError("Can't connect to HTTPS URL because the SSL module is not available.") in this context indicates that when you compiled python3.7 from sources, it wasn't linked against your system OpenSSL library. The pedantic solution would be to ensure you have the required headers besides the lib, with something like:
$ yum install openssl-devel.x86_64 openssl.x86_64
and recompile from scratch, linking against the OpenSSL lib.

但是,正如您正确指出的那样,在yum中安装带有yum的python3(来自Amazon自己的存储库,已针对Amazon Linux测试过的软件包)在EC2中更简单,更可靠,并且可以解决所有依赖性.例如,类似的事情将在Amazon Linux(版本1)中起作用:

However, as you have rightly noted, installing python3 with yum (from Amazon's own repo, packages tested for Amazon Linux) is way easier and more robust approach in EC2, and will resolve all dependencies. For example, something like this is going to work in Amazon Linux (version 1):

$ yum install python36.x86_64
$ yum install python36-pip.noarch

通常,EC2中已经预安装了Python3.6,但python36-pip中尚未预安装

Usually, Python3.6 is already pre-installed in EC2, but not python36-pip...

这是正确的默认行为,它是通过pip3命令调用Python3的pip脚本,而pip是用于Python2的(如果已安装-python2的yum包是python27-pip.noarch),例如:

This is correct and default behavior that Python3's pip script is called by pip3 command, while pip is for Python2 (if installed - the yum package for python2 is python27-pip.noarch), for example:

$ pip3 -V  
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)  
$ pip2 -V  
pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)  
$ pip -V  
pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)  

还有一些版本化的Python命令可与-m开关(用于模块)结合使用,以运行相应版本的pip,例如:
$ python3 -m pip install --user flask

There are also versioned Python commands available in combination with the -m switch (for module) to run corresponding version of pip, for example:
$ python3 -m pip install --user flask

您通过get-pip.py --user安装的计算机可能已放在$ HOME/.local/bin目录中的某个位置. --user开关仅针对当前用户(〜/.local/lib/)而不是针对整个系统安装软件包,这实际上是避免干扰系统python软件包的良好实践.

The one you installed via get-pip.py --user has likely landed somewhere in your $HOME/.local/bin directory. The --user switch installs packages just for the current user (in ~/.local/lib/) rather than for the whole system, which is actually a good practice to avoid interfering with the system python packages.

更好的是,您可以在virtualenv或venv隔离环境中使用pip:有python36-virtualenv(可通过yum安装),以及默认的内置于Python3且易于使用的"venv"模块(python3 -m venv yournewenvname).创建并激活隔离的python环境(目录)后,无论使用什么python版本,都可以在其中执行简单的pip install flask.祝你好运!

Even better, you could use pip inside a virtualenv or venv isolated environments: there is the python36-virtualenv (installable via yum), and the default 'venv' module built-in Python3 and readily available (python3 -m venv yournewenvname). Once you create and then activate your isolated python environment (a directory), you can just do a simple pip install flask there regardless of python version. Good luck!

这篇关于尝试使用Python 3.7.2 pip安装软件包会导致TSL/SSL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆