SSL module in Python is not available (on OSX)

I had a similar problem with Catalina and could not get homebrew reinstall to work. I tried several thing.

brew reinstall openssl
brew reinstall pyenv
brew reinstall pyenv-virtualenv

Ultimately the only thing that worked for me was to completely uninstall both as well as the underlying python installations and then reinstall everything.

brew uninstall pyenv pyenv-virtualenv
brew install pyenv pyenv-virtualenv
pyenv uninstall 3.x.x
pyenv install 3.x.x
pip install -r requirements.txt

The ssl module as well as its underlying C extension appears to be a part of the python formula:

Mac-Admin:~ admin$ python3
Python 3.7.4 (default, Sep  7 2019, 18:27:02) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> ssl
<module 'ssl' from '/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py'>
>>> import _ssl
>>> _ssl
<module '_ssl' from '/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ssl.cpython-37m-darwin.so'>

so it being missing most probably means package installation corruption which brew reinstall python should fix.


Also note that while Homebrew allows multiple versions to coexist, its installation logic isn't quite designed to keep the alternative versions operational unless they are installed via a versioned formula (and e.g. routinely removes old versions in the regular brew cleanup).

So consider using pyenv (also available via brew) if you need to routinely switch between Python versions -- or some 3rd-party tap that offers versioned formulae for it.


This problem might also be, because your python distribution was compiled using the wrong version of openssl.

Support for OpenSSL 1.1.x, was only added in Python 2.7.13, 3.5.3 and 3.6.0 (see https://github.com/pyenv/pyenv/issues/950#issuecomment-562366902)

So if you are trying to install an older version of Python you must first uninstall the new version of openssl with brew and only then you can install an older version of Python with pyenv

brew uninstall --ignore-dependencies [email protected]
pyenv uninstall 3.5.2  # deinstall old versions compiled with the wrong version of openssl
pyenv install 3.5.2

On the other side, if you are trying to install newer version of Python make sure you have installed the latest version of openssl available, before you install them with pyenv:

brew upgrade openssl
pyenv uninstall 3.7.4 # deinstall old versions compiled with the wrong version of openssl
pyenv install 3.7.4

Mac OSX Catalina (and same issue on OSX Mojave) Pyenv

For anyone searching this topic, I had the same presenting problem, but had Python installed via both Homebrew and Pyenv!! It would have been better (IMO) to just use Pyenv to easily manage versions. As mentioned by @ivan_pozdeev in their answer, but here's some detail you might want.

If your situation is similar, none of the above solutions would be quite enough to set things right. Partially I was helped by a Pyenv related answer here: https://stackoverflow.com/a/51797298/3084820 I also happened to have pyenv-virtualenv installed, so mentioning that as well, as it's common to use these two together.

I finally took the following steps to resolve the issue:

brew uninstall python
rm -rf $(pyenv root)
brew uninstall pyenv-virtualenv   # you may not have this installed, but...
brew uninstall pyenv

Now, for a clean installation manageable with Pyenv:

brew install pyenv
pyenv install 3.6.10  (or whatever version you want)

This gave me a clean, working install of Python 3.6.10, and if I wanted or needed to, I could install a different version and switch between with Pyenv.

Tags:

Python

Macos

Ssl