69

After pip install openai, when I try to import openai, it shows this error:

the 'ssl' module of urllib3 is compile with LibreSSL not OpenSSL

I just followed a tutorial on a project about using API of OpenAI. But when I get to the first step which is the install and import OpenAI, I got stuck. And I tried to find the solution for this error but I found nothing.

Here is the message after I try to import OpenAI:

Python 3.9.6 (default, Mar 10 2023, 20:16:38)
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import openai

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/yule/Library/Python/3.9/lib/python/site-packages/openai/__init__.py", line 19, in <module>
    from openai.api_resources import (
  File "/Users/mic/Library/Python/3.9/lib/python/site-packages/openai/api_resources/__init__.py", line 1, in <module>
    from openai.api_resources.audio import Audio  # noqa: F401
  File "/Users/mic/Library/Python/3.9/lib/python/site-packages/openai/api_resources/audio.py", line 4, in <module>
    from openai import api_requestor, util
  File "/Users/mic/Library/Python/3.9/lib/python/site-packages/openai/api_requestor.py", line 22, in <module>
    import requests
  File "/Users/mic/Library/Python/3.9/lib/python/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/Users/mic/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py", line 38, in <module>
    raise ImportError(
ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with LibreSSL 2.8.3. See: https://github.com/urllib3/urllib3/issues/2168

I tried to --upgrade the urllib3, but it is still not working. The result is:

pip3 install --upgrade urllib3
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: urllib3 in ./Library/Python/3.9/lib/python/site-packages (2.0.2)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prismwatermelon
  • 793
  • 1
  • 5
  • 6
  • Is this a Mac-specific problem? Should the question be or not? "darwin" (and to some degree "clang") hints at [macOS](https://en.wikipedia.org/wiki/Darwin_(operating_system)). – Peter Mortensen Aug 14 '23 at 00:27
  • A commenter has [observed the problem on AWS Linux](https://stackoverflow.com/questions/76187256/importerror-urllib3-v2-0-only-supports-openssl-1-1-1-currently-the-ssl-modu#comment134916202_76187415). There are also reports for [RHEL](https://stackoverflow.com/questions/76187256/importerror-urllib3-v2-0-only-supports-openssl-1-1-1-currently-the-ssl-modu#comment134770460_76344709) and [Ubuntu](https://stackoverflow.com/questions/76187256/importerror-urllib3-v2-0-only-supports-openssl-1-1-1-currently-the-ssl-modu#comment135006064_76344709). – Peter Mortensen Aug 14 '23 at 14:11

10 Answers10

108

The reason why the error message mentioned OpenSSL 1.1.1+ and LibreSSL 2.8.3 is that urllib3 v2.0 (the version you've installed) requires OpenSSL 1.1.1+ to work properly, as it relies on some new features of OpenSSL 1.1.1.

The issue is that the version of the 'ssl' module that is currently installed in your environment is compiled with LibreSSL 2.8.3, which is not compatible with urllib3 v2.0.

To use urllib3 v2.0, you need an 'ssl' module compiled with OpenSSL 1.1.1 or later, by trying:

brew install openssl@1.1

Or you could use an older version of urllib3 that is compatible suc. For example urllib3 v1.26.6, which does not have a strict OpenSSL version requirement. You can force the version installing with this command:

pip install urllib3==1.26.6
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Philippe Sisowath
  • 1,400
  • 1
  • 6
  • 11
  • 23
    Unfortunately, the installation of openssl@1.1 did not solve the problem for me. Downgrading urllib3 did the job, but this feels a bit awkward. – Kristian Heitkamp May 10 '23 at 09:35
  • 1
    @KristianHeitkamp Can you tell us which version solved your problem? – Parvathirajan Natarajan May 15 '23 at 10:44
  • Agreed; I had to do both. – John Peurifoy May 16 '23 at 14:10
  • 1
    Sorry, that I have been so unspecific, I ment the second of the above mentioned solutions: pip install urllib3==1.26.6 – Kristian Heitkamp May 16 '23 at 23:15
  • It was to correct urllib3 v2.0 (installed with Python 3.9.6) – Philippe Sisowath May 17 '23 at 10:11
  • 3
    `pip install urllib3==1.26.6` has worked in my case. I had urlib3 2+ version earlier which was the causing the issue. Downgrading does resolve the error. – seahawk May 22 '23 at 08:00
  • 2
    I think the issue with just installing openssl 1.1 is that the python ssl module still isn't going to be compiled against it. So you'd have to compile python from source against openssl 1.1 after installing openssl 1.1. – rich May 31 '23 at 14:13
  • I agreed with @KristianHeitkamp, I uninstalled urllib3 and "pip install urllib3==1.26.6" resolved the problem. Only brew install openssl@1.1 would not change the urllib's properties, would still get the same. LibreSSL is OS X default dependent library of SSL. Can see it from [link](https://security.stackexchange.com/questions/112545/what-are-the-main-advantages-of-using-libressl-versus-openssl) – Yu Chai Jun 01 '23 at 07:13
  • install the eariler urllib version worked for me. – prismwatermelon Jun 15 '23 at 18:57
  • Answer is recommending `brew` when question says nothing about being Mac-specific. I'm seeing this error on AWS Linux. Downgrading pip package is not ideal as some packages require newer urllib3 and keep updating it after my downgrade. Curious what Linux options there are, esp. for those w/o root access. Tried installing from source (e.g., https://help.dreamhost.com/hc/en-us/articles/360001435926-Installing-OpenSSL-locally-under-your-username) but `make test` failed. – sh37211 Jun 20 '23 at 21:27
  • What is *"compatible suc"*? – Peter Mortensen Aug 14 '23 at 00:32
64

I had the same issue while working on a MacBook Air (M1). This did the trick for me

pip uninstall urllib3
pip install 'urllib3<2.0'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tigran
  • 641
  • 1
  • 3
7

I met this problem too. My old version is Python 3.9. "brew install openssl@1.1" doesn't work for me.

You can try:

pipenv install --python 3.11

That fixed my problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Irene
  • 71
  • 3
2

You can try installing an older version of urllib3 that is compatible with the currently installed version of OpenSSL. To do this, you can use pip to install a specific version:

sudo python3.8 -m pip install urllib3==1.26.6
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kabiru Ahmed
  • 166
  • 1
  • 7
  • 1
    `pip install urllib3==1.26.6` would be sufficient. – deadfish Aug 24 '23 at 08:50
  • thanks @deadfish, this resolved the issue. I'm getting this particular warning while fetching the data from py5paisa package and trying to get and preprocess it using pandas. – Navjot Kashi Sep 01 '23 at 14:55
1

For me, the following worked in PyCharm:

  1. At the bottom right: click on Python 3.*
  2. Interpreter Settings
  3. Double click on the left version of urllib3
  4. Check specify version
  5. and finally select 1.26.16 and Install package
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wood4235
  • 11
  • 2
0

You should upgrade your system's LibreSSL version. Use brew upgrade openssl@1.1.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
911
  • 542
  • 2
  • 12
0

We ran into this problem and there were two problems:

  • The urllib3 version is not compatible. -> We removed the current version and tried to install urllib3==1.26.15

Then we ran into the second problem we can't install this version. And we found out Mac Mini uses Z shell (zsh) which didn't allow us to completely install this version of urllib3. We changed to Bash to install and then came back to Z shell. Everything works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Re *"Mac Mini uses Z shell"*: That is dependent on the version of macOS. [Z shell is the default shell in macOS v10.15 (Catalina)](https://en.wikipedia.org/wiki/MacOS_Catalina#Removed_or_changed_components) and later. – Peter Mortensen Aug 14 '23 at 00:40
0

This is because of a dependency mismatch. Let's say, you have installed a Python package called 'x', and it depends on urllib3. As you may know, urllib3 depends on OpenSSL. Something like this.

x > urllib3 > openssl

When you install x using pip, it will install all x's dependencies, including urllib3. If the installed urllib3 version doesn't support the OpenSSL version, you will get this error.

To fix this, you may downgrade x until this error is fixed.

I found this solution from here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shirantha Madusanka
  • 1,461
  • 1
  • 11
  • 16
0

On my mac this is how the problem is resolved for me :

  1. download python(3.9.6) from https://www.python.org/downloads/macos/ This package includes its own private copy of OpenSSL 1.1.1.

  2. run command "Update Shell Profile.command" from your new python installation. In my case "/Applications/Python 3.9/Update Shell Profile.command"

Now you can use interpreter in /usr/local/bin/python3

Pallav
  • 332
  • 3
  • 8
-1

MacPorts version 3.9 compiled with OpenSSL and works properly. Install MacPorts and use Python from it. It is working on my M2.

Python 3.9 (MacPorts package)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131