使用 OpenSSL 时出现 SSL 后端错误

我尝试用 pip 将 pycurl 安装到一个 Virtual alenv 中,得到了这个错误

ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl backend (none/other)

我读了一些文档说,“要解决这个问题,您需要告诉 setup.py 使用了哪个 SSL 后端” (资料来源)虽然我不知道如何做到这一点,因为我安装 pycurl 使用 pip。

在使用 pip 安装 pycurl 时,如何指定 SSL 后端?

谢谢

64644 次浏览

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:

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.

for most people

After reading their INSTALLATION file, I was able to solve my problem by setting an environment variable and did a reinstall

# remove existing `pycurl` installation
pip uninstall pycurl


# export variable with your link-time ssl backend (which is openssl above)
export PYCURL_SSL_LIBRARY=openssl


# then, re-install `pycurl` with **no cache**
pip install pycurl --no-cache-dir

There could be other solution out there but this works perfectly for me on a virtualenv and pip installation.

Some people have a different error message complaining about nss instead of openssl

ImportError: pycurl: libcurl link-time ssl backend (nss)

(the key part is nss) so you have to do something different for this error message:

pip uninstall pycurl
pip install --no-cache-dir --compile --compile-options="--with-nss" pycurl

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):

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

helloworld2013's answer is correct, but the key is matching the SSL library that pycurl is expecting. The error will be something like:

pycurl: libcurl link-time ssl backend (<library>) is different from compile-time ssl backend (<library> or "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:

pip uninstall pycurl
export PYCURL_SSL_LIBRARY=nss
pip install pycurl

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:

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.

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.

2019 Edit: Doing a sudo pip install might 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.

With pip 7.1 you can put the following in your requirements file:

pycurl==7.19.5.1 --global-option="--with-nss"

Simply replace nss with the relevant ssl backend library.

I am running this on OS X and some of the above solutions weren't working. Similar to Edward Newell's comment the PYCURL_SSL_LIBRARY variable seemed to have been completely ignored.
Further reading of the PycURL installation doc revealed the following:

pip may reinstall the package it has previously compiled instead of recompiling pycurl with newly specified options

Therefore, I had to force it to compile with:

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:

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 ssl and not openssl. If you specify --with-openssl you'll get an error. Also note that if you were messing around with the PYCURL_SSL_LIBRARY variable 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.

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:

pip uninstall pycurl
export PYCURL_SSL_LIBRARY=[nss|openssl|ssl|gnutls]
pip install pycurl --no-cache-dir

For python 2.7

sudo apt-get install build-essential libssl-dev libffi-dev python-dev

For python 3.5 also install the next:

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#downloads and run the next command:

python setup.py --with-openssl install

Also you can do it into python environment:

(test_env)user@pc:~/Downloads/pycurl-7.43.0$ python setup.py --with-openssl install

Reinstallation of 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.

At start:

'PycURL/7.43.0 libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3'

Part 1.Re/Installation with pip

Firstly I have removed pycurl from virtualenv using pip as was suggested previous answers:

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.

I also tried to recompile pycurl with pip using:

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)

Part 2. Installation from tar

After previous method didn't work I have decidede to install pycurl from tar with:

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.

Part 3. Reinstallation of curl and curllib

Finally I understood that pycurl doesn't installs normally into environment because global curl and libcurl are compiled with gnutls.

Before starting check it with:

curl-config --configure

One of the output lines will be

'--without-ssl' '--with-gnutls'

To recompile it:

Firstly remove curl:

sudo apt-get purge curl

Install any build dependencies needed for curl

sudo apt-get build-dep curl

Get latest (as of Dec 20, 2016) 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:

curl version: 7.51.0
Host setup: x86_64-pc-linux-gnu
Install prefix: /usr/local
Compiler: gcc
SSL support: enabled (OpenSSL) ...

Resolve any issues of C-level lib location caches ("shared library cache")

sudo ldconfig

Now try to reinstall pycurl within environment:

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.2g zlib/1.2.8 librtmp/2.3'

pip install -U pip


if [ "$(curl --version | grep NSS 2>/dev/null)" ]; then
pip install --compile --install-option="--with-nss" pycurl
else
pip install --compile --install-option="--with-openssl" pycurl
fi

I had this problem for days. Finally with the help of other answers here (mainly Alexander Tyapkov's) I got it working for AWS Elastic Beanstalk.

Manual install (connecting with SSH):

sudo pip uninstall pycurl
curl -O https://pypi.python.org/packages/source/p/pycurl/pycurl-7.43.0.tar.gz
sudo pip install pycurl-7.43.0.tar.gz --global-option="--with-nss"

IMPORTANT: Please note that you have to make sure you are using the currect version of Python and PIP, otherwise you might be compiling it for Python 2.x and using v3.x.

Auto-install in Elastic Beanstalk:

files:
"/usr/local/share/pycurl-7.43.0.tar.gz" :
mode: "000644"
owner: root
group: root
source: https://pypi.python.org/packages/source/p/pycurl/pycurl-7.43.0.tar.gz


commands:
01_download_pip3:
# run this before PIP installs requirements as it needs to be compiled with OpenSSL
command: 'curl -O https://bootstrap.pypa.io/get-pip.py'
02_install_pip3:
# run this before PIP installs requirements as it needs to be compiled with OpenSSL
command: 'python3 get-pip.py'
03_pycurl_uninstall:
# run this before PIP installs requirements as it needs to be compiled with OpenSSL
command: '/usr/bin/yes | sudo pip uninstall pycurl'
04_pycurl_download:
# run this before PIP installs requirements as it needs to be compiled with OpenSSL
command: 'curl -O https://pypi.python.org/packages/source/p/pycurl/pycurl-7.43.0.tar.gz'
05_pycurl_reinstall:
# run this before PIP installs requirements as it needs to be compiled with OpenSSL
command: 'sudo pip install pycurl-7.43.0.tar.gz --global-option="--with-nss"'


container_commands:
09_pycurl_reinstall:
# run this before PIP installs requirements as it needs to be compiled with OpenSSL
# the upgrade option is because it will run after PIP installs the requirements.txt file.
# and it needs to be done with the virtual-env activated
command: 'source /opt/python/run/venv/bin/activate && pip3 install /usr/local/share/pycurl-7.43.0.tar.gz --global-option="--with-nss" --upgrade'

I had this problem because I was trying to configure Celery 4 with Django 1.10 in Elastic Beanstalk. If that's your case, I wrote a full blog post about it.

I encountered this problem and Sanket Jagtap's answer worked for me. I tried the answer with the most votes answer but it did not work.

My openssl old version is 1.0.1t, I think reinstalling openssl may solve this problem.

--- pycurl's openssl backend time....

I rebuilt the latest openssl and tried this answer. Check this out.

pip install --compile --install-option="--with-openssl" pycurl

This worked for me.

i recommend we should reinstall our openssl for try..

With macOS 10.13, a brew-installed openSSL, and virtualenv, I was successful with:

# cd to your virtualenv, then…
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=openssl
export LDFLAGS=-L/usr/local/opt/openssl/lib
export CPPFLAGS=-I/usr/local/opt/openssl/include
pip install pycurl --compile --no-cache-dir

Error:

ImportError: pycurl: libcurl link-time ssl backend (openssl) is different from compile-time ssl backend (none/other)

This worked for me, Mac 10.13, python 3.5, pycurl import worked after installing like this

pip3 uninstall pycurl;


pip3 install --compile --install-option="--with-openssl" pycurl

The method to fix the pycurl after Mac OS High Sierra update:

  1. Reinstall the curl libraries to use OpenSSL instead of SecureTransport

    brew install curl --with-openssl
    
  2. Install pycurl with correct build-time environment and paths

    export PYCURL_SSL_LIBRARY=openssl
    pip uninstall pycurl
    pip install --no-cache-dir --global-option=build_ext --global-option="-L/usr/local/opt/openssl/lib" --global-option="-I/usr/local/opt/openssl/include" --user pycurl
    

Following worked for me with Python3.6

MacOS High-Sierra

sudo pip3 uninstall pycurl
sudo pip3 install --compile --install-option="--with-openssl" pycurl

CentOS 7

sudo pip3 uninstall pycurl
sudo pip3 install --compile --install-option="--with-nss" pycurl

After being stuck on this for a long time, I found out that apple stopped including OpenSSL headers since OS X 10.11 El Capitan. how to fix?

1) brew install openssl


2) echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile (or .zshrc for zsh, etc)


3) pip uninstall pycurl


4) pip install --install-option="--with-openssl" --install-option="--openssl-dir=/usr/local/opt/openssl" pycurl

This link sums up the reason why the errors occur and gives a clear instruction to fix the problem.

https://cscheng.info/2018/01/26/installing-pycurl-on-macos-high-sierra.html

For me, the problem occurred when I upgraded to High-Sierra from El Captain.

export CPPFLAGS=-I/usr/local/opt/openssl/include
export LDFLAGS=-L/usr/local/opt/openssl/lib


pip install pycurl --global-option="--with-openssl"

This worked for me:

pip install --compile --install-option="--with-openssl" pycurl

I tried everything here on macOS 10.13 with no success. Then I found https://gist.github.com/webinista/b4b6a4cf8f158431b2c5134630c2cbfe which worked:

brew install curl --with-openssl
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=openssl
export LDFLAGS=-L/usr/local/opt/openssl/lib;export CPPFLAGS=-I/usr/local/opt/openssl/include; pip install pycurl --compile --no-cache-dir

This worked for me both when not using a virtualenv and within a virtualenv.

For anyone having problem inside PyCharm CE on macOS Mojave this is how i got it working in venv:

  • specify version: 7.43.0.1
  • Options: --install-option=--with-openssl --install-option=--openssl-dir=/usr/local/opt/openssl

PyCharm Project Interpreter screen shot

Same problem on amazonlinux - solved
I had this problem while creating a docker image based on amazonlinux, installing python3.7 and adding the pycurl module. All other python modules were installed correctly except pycurl. After trying many of the solutions proposed in the threads linked to this problem I finally solved my problem by using following commands for installation of all the pieces.
yum -y install python3 python3-devel gcc libcurl-devel aws-cli openssl-static.x86_64
then installed other modules like psycopg2-binary, requests, certifi using:
pip3 install --user --no-cache-dir -r requirements.txt

and finally installed pycurl module using:

pip3 install --user --global-option="--with-openssl" --no-cache-dir pycurl
and passing here the openssl global option. The installation of the static library openssl-static.x86_64 solved the problem in my case as using global option used by the second pip3 command.

FWIW, I ran into a lot of issues getting this working via AWS Elastic Beanstalk and finally was able to get it working with:

packages:
yum:
openssl-devel: []
libcurl-devel: []


container_commands:
# Reinstall PyCurl with correct ssl backend
05_reinstall_pycurl:
command: |
pip install --upgrade pip
pip uninstall -y pycurl
pip install --global-option='--with-openssl' pycurl

Recently while upgrading a Django project I had the similar error. But this time setting the environment variable did not work. So I had to set both environment variable export PYCURL_SSL_LIBRARY=openssl and pass the flag --global-option="with-openssl".

The original answer was posted on this page