Python 使用 OpenSSL 时出现 SSL 后端错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21096436/
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
SSL backend error when using OpenSSL
提问by helloworld2013
I was trying to install pycurl in a virtualenv using pip and I got this error
我试图使用 pip 在 virtualenv 中安装 pycurl,但出现此错误
ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl backend (none/other)
I read some documentation saying that "To fix this, you need to tell setup.py what SSL backend is used"(source)although I am not sure how to do this since I installed pycurl using pip.
我读了一些文档说“要解决这个问题,你需要告诉 setup.py 使用了什么 SSL 后端” (来源),尽管我不知道如何做到这一点,因为我使用 pip 安装了 pycurl。
How can I specify the SSL backend when installing pycurl with pip?
使用pip安装pycurl时如何指定SSL后端?
Thanks
谢谢
采纳答案by helloworld2013
After reading their INSTALLATION file, I was able to solve my problem by setting an environment variable and did a reinstall
在阅读了他们的 INSTALLATION 文件后,我能够通过设置环境变量并重新安装来解决我的问题
remove existing
pycurlinstallationpip uninstall pycurlexport variable with your link-time ssl backend (which is openssl above)
export PYCURL_SSL_LIBRARY=opensslthen, re-install
pycurlwith no cachepip install pycurl --no-cache-dir
删除现有
pycurl安装pip uninstall pycurl使用链接时 ssl 后端导出变量(上面是 openssl)
export PYCURL_SSL_LIBRARY=openssl然后,
pycurl在没有缓存的情况下重新安装pip install pycurl --no-cache-dir
There could be other solution out there but this works perfectly for me on a virtualenvand pipinstallation.
可能有其他的解决方案在那里,但这个工程完美的我在virtualenv和pip安装。
回答by Sabuj Hassan
You can download the tar.gz file from here. Then extract it into a folder. You'll find a setup.py file there. Run the command over there that the site mentioned. For example:
您可以从这里下载 tar.gz 文件。然后将其解压缩到一个文件夹中。你会在那里找到一个 setup.py 文件。在那里运行该站点提到的命令。例如:
python setup.py --with-[ssl|gnutls|nss] install
FYI: I tried to install pycurl at my windows, but I couldn't. But did it on my linux.
仅供参考:我试图在我的 Windows 上安装 pycurl,但我不能。但是在我的linux上做到了。
回答by JoelBondurant
This worked for me:
这对我有用:
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=nss
easy_install pycurl
None of this worked for me (note the difference is simply easy_install vs pip):
这些都不适合我(注意区别只是easy_install vs pip):
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=[nss|openssl|ssl|gnutls]
pip install pycurl
#xor
curl -O https://pypi.python.org/packages/source/p/pycurl/pycurl-7.19.3.1.tar.gz
#...
python setup.py --with-[nss|openssl|ssl|gnutls] install
回答by DrStrangepork
helloworld2013's answer is correct, but the key is matching the SSL library that pycurl is expecting. The error will be something like:
helloworld2013 的答案是正确的,但关键是匹配 pycurl 期望的 SSL 库。错误将类似于:
pycurl: libcurl link-time ssl backend (<library>) is different from compile-time ssl backend (<library>or "none/other")
pycurl:libcurl 链接时 ssl 后端(<library>)与编译时 ssl 后端(<library>或“ none/other”)不同
To fix it, you have to use the library pycurl is expecting. In my case, my error was "pycurl: libcurl link-time ssl backend (nss) is different from compile-time ssl backend (openssl)", so my fix was:
要修复它,您必须使用 pycurl 期望的库。就我而言,我的错误是“ pycurl:libcurl 链接时 ssl 后端(nss)与编译时 ssl 后端(openssl)不同”,所以我的修复是:
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=nss
pip install pycurl
回答by alfonso
I'm on CentOS 7. I tried all of the above and couldn't get anything to work. It turns out I needed to run these as a root user. So if you're having trouble, try any of the above solutions as a root user. As an example, here is what worked for me:
我在 CentOS 7 上。我尝试了上述所有方法,但无法正常工作。事实证明,我需要以 root 用户身份运行这些。因此,如果您遇到问题,请以 root 用户身份尝试上述任何解决方案。例如,这对我有用:
su -
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=[nss|openssl|ssl|gnutls]
pip install pycurl
Of course, all the usual disclaimers about running as a root user apply.
当然,所有关于以 root 用户身份运行的常见免责声明都适用。
Note: [nss|openssl|ssl|gnutls] in the code above means to pick one, and don't include the square brackets or pipes. The person who asked the original question would have chosen openssl. In my particular case, I chose nss. Your error message should tell you which choice to make.
注意:上面代码中的 [nss|openssl|ssl|gnutls] 表示选择一个,不包括方括号或管道。提出原始问题的人会选择 openssl。在我的特殊情况下,我选择了 nss。您的错误消息应该告诉您做出哪个选择。
2019 Edit: Doing a sudo pip installmight cause a problem with the machine's system install of Python. Perhaps try working in a Python virtual environment and install the packages there. If that doesn't work, the sudo trick in my answer is probably one of the last options to consider.
2019 年编辑:执行 asudo pip install可能会导致机器的 Python 系统安装出现问题。也许尝试在 Python 虚拟环境中工作并在那里安装软件包。如果这不起作用,我的答案中的 sudo 技巧可能是最后要考虑的选项之一。
回答by maharg101
With pip 7.1 you can put the following in your requirements file:
使用 pip 7.1,您可以将以下内容放入您的需求文件中:
pycurl==7.19.5.1 --global-option="--with-nss"
Simply replace nss with the relevant ssl backend library.
只需将 nss 替换为相关的 ssl 后端库。
回答by Gunga
I am running this on OS X and some of the above solutions weren't working. Similar to Edward Newell's commentthe PYCURL_SSL_LIBRARYvariable seemed to have been completely ignored.
Further reading of the PycURL installation docrevealed the following:
我在 OS X 上运行它,上面的一些解决方案不起作用。与Edward Newell 的评论类似,该PYCURL_SSL_LIBRARY变量似乎已被完全忽略。
进一步阅读PycURL 安装文档揭示了以下内容:
pip may reinstall the package it has previously compiled instead of recompiling pycurl with newly specified options
pip 可能会重新安装它之前编译的包,而不是使用新指定的选项重新编译 pycurl
Therefore, I had to force it to compile with:
因此,我不得不强制它编译:
pip install --compile pycurl
pip install --compile pycurl
That works on a number of cases. However, I did run into a few systems that continued to ignore the variable so, similar to maharg101's answer, I resorted to the install options which through pip can be set like this:
这适用于许多情况。但是,我确实遇到了一些继续忽略变量的系统,因此类似于maharg101 的回答,我求助于安装选项,通过 pip 可以这样设置:
pip install pycurl --global-option="--with-[ssl|gnutls|nss]"
pip install pycurl --global-option="--with-[ssl|gnutls|nss]"
where you select one of the three options inside the square brackets. Notice that the available option is ssland notopenssl. If you specify --with-opensslyou'll get an error. Also note that if you were messing around with the PYCURL_SSL_LIBRARYvariable and switching it to funky values to see what would happen this last command will definitely catch it and throw an error if the value is set but not valid.
您可以在方括号内选择三个选项之一。请注意,可用选项是ssl而不是openssl。如果你指定--with-openssl你会得到一个错误。另请注意,如果您正在处理PYCURL_SSL_LIBRARY变量并将其切换为时髦的值以查看会发生什么,那么最后一个命令肯定会捕获它并在该值已设置但无效时抛出错误。
回答by Robert Kelly
Not sure if this is because of running in a virtualenv, but on CentOS 7 these solutions weren't working for me; the compiled objects were still being grabbed from the cache dir when I was reinstalling. If you're running into the same problem after trying other solutions here, try the following:
不确定这是不是因为在 virtualenv 中运行,但在 CentOS 7 上这些解决方案对我不起作用;当我重新安装时,编译的对象仍在从缓存目录中获取。如果您在尝试其他解决方案后遇到同样的问题,请尝试以下操作:
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=[nss|openssl|ssl|gnutls]
pip install pycurl --no-cache-dir
回答by Andrew Nodermann
For python 2.7
对于python 2.7
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
For python 3.5 also install the next:
对于 python 3.5 还安装下一个:
sudo apt-get install python3.5-dev
Download the latest pycurl-7.43.0.tar.gz (md5) Source from pypi https://pypi.python.org/pypi/pycurl/7.43.0#downloadsand run the next command:
从 pypi https://pypi.python.org/pypi/pycurl/7.43.0#downloads下载最新的 pycurl-7.43.0.tar.gz (md5) 源 并运行下一个命令:
python setup.py --with-openssl install
Also you can do it into python environment:
您也可以在python环境中执行此操作:
(test_env)user@pc:~/Downloads/pycurl-7.43.0$ python setup.py --with-openssl install
回答by Alexander Tyapkov
Reinstallation of curl
重新安装curl
I tried every suggestion from this discussion but no one worked for me. As solution I have reinstalled curl and curlib. After that I was able to install pycurl with ssl support inside environment.
我尝试了这次讨论中的每一个建议,但没有人为我工作。作为解决方案,我重新安装了 curl 和 curlib。之后,我能够在环境中安装带有 ssl 支持的 pycurl。
At start:
开始时:
'PycURL/7.43.0 libcurl/7.47.0 GnuTLS/3.4.10zlib/1.2.8 libidn/1.32 librtmp/2.3'
'PycURL/7.43.0 libcurl/7.47.0 GnuTLS/3.4.10zlib/1.2.8 libidn/1.32 librtmp/2.3'
Part 1.Re/Installation with pip
第 1 部分。使用 pip 重新/安装
Firstly I have removed pycurl from virtualenv using pip as was suggested previous answers:
首先,我已经使用 pip 从 virtualenv 中删除了 pycurl,正如之前的答案所建议的那样:
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=openssl
pip install pycurl --global-option="--with-openssl"
The idea here is that package was cached and we just reintstall it with openssl option.
这里的想法是包被缓存,我们只是用 openssl 选项重新安装它。
I also tried to recompile pycurl with pip using:
我还尝试使用 pip 重新编译 pycurl:
pip install pycurl --compile pycurl --no-cache
..but had the same error after running:
..但运行后有同样的错误:
python
import pycurl
pycurl.version
ImportError: pycurl: libcurl link-time ssl backend (gnutls) is different from compile-time ssl backend (openssl)
ImportError: pycurl: libcurl 链接时 ssl 后端 ( gnutls) 与编译时 ssl 后端 ( openssl) 不同
Part 2. Installation from tar
第 2 部分。 从 tar 安装
After previous method didn't work I have decidede to install pycurl from tar with:
在以前的方法不起作用之后,我决定从 tar 安装 pycurl:
curl -O https://pypi.python.org/packages/source/p/pycurl/pycurl-7.43.0.tar.gz
sudo tar -xzvf pycurl-7.43.0.tar.gz
cd pycurl-7.43.0/
sudo python setup.py --with-ssl install
It has installed pycurl globally but not within virtualenv. I also didn't check if it was installed with SSL support or not but think that still without ssl.
它已全局安装 pycurl,但未在 virtualenv 中安装。我也没有检查它是否安装了 SSL 支持,但认为仍然没有 ssl。
Part 3. Reinstallation of curl and curllib
第 3 部分. 重新安装 curl 和 curllib
Finally I understood that pycurl doesn't installs normally into environment because global curl and libcurl are compiled with gnutls.
最后我明白 pycurl 不能正常安装到环境中,因为全局 curl 和 libcurl 是用 gnutls 编译的。
Before starting check it with:
在开始之前检查它:
curl-config --configure
One of the output lines will be
输出线之一将是
'--without-ssl' '--with-gnutls'
'--without-ssl' '--with-gnutls'
To recompile it:
要重新编译它:
Firstly remove curl:
首先删除卷曲:
sudo apt-get purge curl
Install any build dependencies needed for curl
安装 curl 所需的任何构建依赖项
sudo apt-get build-dep curl
Get latest (as of Dec 20, 2016) libcurl
获取最新的(截至 2016 年 12 月 20 日)libcurl
mkdir ~/curl
wget http://curl.haxx.se/download/curl-7.51.0.tar.bz2
tar -xvjf curl-7.51.0.tar.bz2
cd curl-7.51.0
The usual steps for building an app from source
从源代码构建应用程序的通常步骤
./configure
./make
sudo make install
If openssl installed correctly then configure will find it automatically. The output will be:
如果 openssl 安装正确,那么 configure 会自动找到它。输出将是:
curl version: 7.51.0
Host setup: x86_64-pc-linux-gnu
Install prefix: /usr/local
Compiler: gcc
SSL support: enabled (OpenSSL)...
curl 版本:7.51.0
主机设置:x86_64-pc-linux-gnu
安装前缀:/usr/local
编译器:gcc
SSL 支持: 启用 (OpenSSL)...
Resolve any issues of C-level lib location caches ("shared library cache")
解决 C 级库位置缓存(“共享库缓存”)的任何问题
sudo ldconfig
Now try to reinstall pycurl within environment:
现在尝试在环境中重新安装 pycurl:
curl -O https://pypi.python.org/packages/source/p/pycurl/pycurl-7.43.0.tar.gz
pip install pycurl-7.43.0.tar.gz --global-option="--with-openssl"
The result should be:
结果应该是:
python
import pycurl
pycurl.version
'PycURL/7.43.0 libcurl/7.51.0 OpenSSL/1.0.2gzlib/1.2.8 librtmp/2.3'
'PycURL/7.43.0 libcurl/7.51.0 OpenSSL/1.0.2gzlib/1.2.8 librtmp/2.3'

