How to Compile Python with OpenSSL on Mac

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

You probably have already successfully installed Python from source on your Mac, but are running into an SSL error. Some Google searching has told you that you need to compile Python with OpenSSL support on your Mac. But how the heck do you do that in Python 3?

Python with SSL on Mac

Let’s first take a look at the two common errors that indicate that you must build Python from source with OpenSSL support on Mac.

1.) You try to install a package via pip and you get a Can’t connect to HTTPS URL because the SSL module is not available error:

$ pip install pandas
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting pandas
    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/pandas/
    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/pandas/
    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/pandas/
    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/pandas/
    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/pandas/
Could not fetch URL https://pypi.org/simple/pandas/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pandas/ (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 pandas (from versions: )
No matching distribution found for pandas

Cant’t connect to HTTPS URL because the SSL module is not available error

2.) You try to import ssl in Python 3 and you see a No module named _ssl error like below:

>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/User/tony/opt/py36/lib/python3.6/ssl.py", line 101, in <module>
    import _ssl      # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'

ModuleNotFoundError No module named '_ssl'

The problem is because the version of openssl that ships with Mac does not expose the headers and libraries necessary for Python to hook into.  You may have been able to install Python from source on Mac without any errors, but it doesn’t have SSL support so you aren’t able to pip install anything from pypi.org or import the SSL Python package.

Compiling Python with OpenSSL Support

Unfortunately, you will have to recompile Python.  Assuming that you have Homebrew installed, make sure that you have the latest Homebrew version of openssl installed.

$ brew install openssl

Find the location of the openssl prefixes in brew with the following command.

$ brew --prefix openssl
/usr/local/opt/openssl

Recompile Python and explicitly tell it where to find openssl with CPPFLAGS and LDFLAGS.

./configure CPPFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib" 
make
make install

After the make install finishes, you will have a compiled version of Python with OpenSSL.  You should have no problem importing the ssl Python package or installing Python package via pip.

Let me know if you have any questions in comments below. I’d be more than happy to do my best to help you out.

And don’t forget to check out my other Python tutorials here.

Happy coding!


About The Author

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

11 thoughts on “How to Compile Python with OpenSSL on Mac”

  1. Hi Tony,

    This has been a life saver thanks so much! A couple of places I got tripped up

    1) I think the code in your vid saves Python to /usr/local/bin, although you then later reference Users/tonyf..

    2) I had this the issue with virtualenv described here: https://stackoverflow.com/questions/39964635/error-virtualenv-command-not-found-but-install-location-is-in-pythonpath?noredirect=1&lq=1

    I fixed it by using this command python -m virtualenv ~/env/py37 -p /usr/local/bin/python3

    Hope that is helpful.

    Cheers,

    Linz

    Reply
    • Appreciate you for taking the time to share this. I’m sure it will be helpful to others who run into this virtualenv issue.

      Reply

Leave a Comment