Resolving ImportError: urllib3 v2.0 Requires OpenSSL 1.1.1+
Written on
Understanding the ImportError
Recently, many Python developers have faced an ImportError when trying to import packages that rely on urllib3 and OpenSSL, such as the openai package. This specific error highlights that the recent versions of urllib3 only function with OpenSSL versions 1.1.1 or higher. The error message reads:
ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with LibreSSL 2.8.3.
In this guide, we will replicate the error and then outline steps to rectify it. Let’s dive in!
Reproducing the Error
To begin, we will create and activate a virtual environment for installing the necessary Python dependencies.
# Create a virtual environment
$ python3 -m venv ~/test-venv
# Activate the newly created virtual environment
$ source ~/test-venv/bin/activate
Next, we will install specific versions of dependencies that will aid in recreating the error that brought you here.
# Install the openai package
$ pip install openai
Finally, we can initiate a Python interactive session and attempt to import the openai package:
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
If you are using Python 3.9, you will likely encounter the following error:
Traceback (most recent call last):
File "", line 1, in
File "/Users/yule/Library/Python/3.9/lib/python/site-packages/openai/__init__.py", line 19, in
from openai.api_resources import (...
ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with LibreSSL 2.8.3.
Why This Error Occurs and How to Fix It
The issue arises because if you have urllib3 version 2.0 installed with Python 3.9, it requires OpenSSL 1.1.1 or higher due to the use of features introduced in that version. However, your current ssl module is compiled using LibreSSL 2.8.3, which is incompatible with urllib3 v2.0+.
To resolve this, you must compile the ssl module using OpenSSL v1.1.1+. This can be done via Homebrew on macOS:
$ brew install [email protected]
If the above solution does not resolve the issue, you can opt for an older version of urllib3 that is compatible with your existing OpenSSL setup. Version 1.26.6 does not enforce a specific OpenSSL version requirement:
$ python3 -m pip install urllib3==1.26.6
Final Thoughts
In this brief tutorial, we explored an ImportError that many Python developers have been encountering recently. We replicated the error reported by urllib3 version 2.0 in an environment with outdated OpenSSL dependencies and provided several solutions to help you resolve the issue.
This video explains the limitations of urllib3 v2.0 regarding OpenSSL compatibility and how to address these issues in your Python environment.
This video covers troubleshooting the SSL module in Python and solutions for installing Python packages effectively.