39

I'm having trouble running pip install in a virtualenv on OSX 10.13. I already have run brew install openssl and the path /usr/local/include/openssl points to ../opt/openssl/include/openssl. Does anyone know how to fix this? This started happening after I reinstalled python using brew install.

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Collecting Pillow Could not fetch URL https://pypi.python.org/simple/pillow/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping Could not find a version that satisfies the requirement Pillow (from versions: ) No matching distribution found for Pillow

UPDATE: Here's some more info:

✗ which python
/usr/local/opt/python/libexec/bin/python
✗ which pip
/usr/local/opt/python/libexec/bin/pip
✗ python --version
Python 3.7.4
✗ pip --version
pip 19.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
✗ brew info python
python: stable 3.7.4 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python/3.6.5_1 (4,795 files, 100.0MB)
  Poured from bottle on 2019-10-08 at 14:39:37
/usr/local/Cellar/python/3.7.4_1 (3,903 files, 60.6MB) *
  Poured from bottle on 2019-10-08 at 14:37:10
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/python.rb

Yes, I have both 3.6.5_1 and 3.7.4_1 installed as I may need to switch between the two at times.

✗ brew unlink openssl
Unlinking /usr/local/Cellar/openssl/1.0.2s... 0 symlinks removed
John M.
  • 2,642
  • 7
  • 26
  • 55
  • Start with showing the output of `which python`, `which pip`, `python --version`, `pip --version`. Then, last time I checked, `openssl` was keg-only, so `/usr/local/include/openssl` shouldn't be present, run `brew unlink openssl`. – ivan_pozdeev Oct 08 '19 at 05:18
  • @ivan_pozdeev Sure, I've added the output. I've already tried `brew unlink`. – John M. Oct 08 '19 at 05:27

9 Answers9

80

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.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
35

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.

Matt Morgan
  • 4,900
  • 4
  • 21
  • 30
  • I think I am in the same situation. Now, is it necessary to `brew uninstall python` if you are just using `pyenv`? Or does this create a conflict somewhere? Also, I tried `brew uninstall python` and it threw me the error message "Error: Refusing to uninstall /usr/local/Cellar/python/3.7.7 because it is required by [a bunch of other packages]". It is probably safe to go ahead with the suggestion of `brew uninstall --ignore-dependencies python`, but I am hesitant nevertheless. I cannot remember how I used this version of python, and if somehow I am still using it. – Antoine Mar 29 '20 at 14:18
  • 5
    @Antoine, I believe I was able to fix this in pyenv without reinstall python with brew. I did however reinstall a specific python version with pyenv: `pyenv uninstall 3.6.6` `pyenv install 3.6.6` `pyenv global 3.6.6` – wronk Mar 29 '20 at 22:40
  • @wronk thanks for your input! It seems (but I am not entirely sure) that the problem that I had somehow disappeared by running `brew uninstall python` **even though** it threw the error that it wouldn't uninstall python. Maybe the command did some preliminary cleaning anyway, before deciding that it wouldn't go ahead with the uninstall of python? Or maybe there is something else I did. At any rate, I am still having a **different** issue with another version of python, and I think I will reinstall that version as you suggest. – Antoine Mar 30 '20 at 09:16
  • FWIW: I can confirm what @wronk mentions worked for me as well, being reinstall pyenv, as mentioned, without having to touch the python installed with brew. – Juliën Jun 22 '20 at 14:55
  • @matt-morgan thank you for this post. Please make sure if you really have to uninstall python and all this staff. Same as @wronk I'm was able to fix this just by installing another version of python. For instance I had this problem on python `3.7.4` so I've just installed `3.7.5` – khashashin Mar 23 '21 at 13:45
7

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 openssl@1.1
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
asmaier
  • 11,132
  • 11
  • 76
  • 103
  • I have about 20 virtualenvs. Moving the `.pyenv/versions/x.x.x/envs` directories to a temporary location, I was able to uninstall/reinstall just the python versions to fix openssl and then move the envs back and have them working properly again – Arne Claassen Nov 07 '20 at 17:53
5

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
  • does `brew uninstall pyenv pyenv-virtualenv` NOT remove all virtual environments as suggested by your `pyenv uninstall 3.x.x`? – Antoine Mar 29 '20 at 13:55
  • @Antoine no, it does not. However, probably, in this case, you also need to remove them too, manually. Also, using requirements.txt file is very, very useful, and will save you on multiple occasions, since these development python environments tend to break from time to time. – Kosiek Aug 27 '20 at 11:37
5

My fix is to reinstall pyenv and python

 brew uninstall pyenv pyenv-virtualenv
 brew install pyenv pyenv-virtualenv
 pyenv uninstall 3.6.5
 pyenv install 3.6.5
tzatalin
  • 404
  • 4
  • 7
2

I also had this error and I fixed it with brew update && brew upgrade

baskcat
  • 135
  • 1
  • 9
1

I had the same error and it was because I was using python 3.6.5 in my pyenv environment. The below treatment worked for me.

pyenv install 3.7.3
pyenv global 3.7.3
0

I had the same error. I tried re-installing OpenSSL. That didn't help. I finally went to https://www.python.org/ and downloaded the latest official/stable installer and ran it. That fixed my issues.

That was probably overkill but it resolved the issue.

Marcus Talcott
  • 321
  • 2
  • 4
0

I was also having this problem on macOS Monterey and Anaconda. The SSL module could be found in the base environment, but in another environment, it would not be found.

I was able to fix this by doing the following:

conda activate my_env
conda update --all

After this, I started getting a new error that libzmq was not found, so I installed Zeromq, as stated in Anaconda.org

conda install -c anaconda zeromq

Restarted the terminal, and now everything works in the environment!