Install and run Mac Python Open CV

First, you need to decide whether you want to run Python2 or Python 3. I would advocate Python 3, firstly because this is clearly a new project so you may as well use the latest and greatest Python, and secondly since Python 2 is end-of-lifed in 9 days’ time.

Then you need to decide if you want to use the Apple-supplied Python, in /usr/bin or the homebrew supplied Python. As you have installed homebrew Python, I would advocate using the homebrew one because you can delete the whole thing and re-install it if you mess it up while all the Apple-supplied patches, OS upgrades and their other uses of their Python will remain unaffected. Happy days!

So, you want to use homebrew Python 3. Now check what homebrew tells you, by running:

brew info python

Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been
installed into /usr/local/opt/python/libexec/bin

That means if you want to run python and pip (without a version number, as opposed to python3 and pip3) and expect that to start Python 3 and its corresponding pip, you need to put /usr/local/opt/python/libexec/bin at the start of your PATH in your bash profile (probably $HOME/.bash_profile):

export PATH=/usr/local/opt/python/libexec/bin:$PATH

Then start a new Terminal and check which Python runs when you type python:

type python

And it must report as follows if you are on the right track:

python is /usr/local/opt/python/libexec/bin/python

Then, you can check which Python version that is with:

python -V

and it should be:

Python 3.7.5

Now you can install OpenCV with:

pip install opencv-python

Then load it in the interpreter with:

import cv2

If, for some reason, you want to run the old v2.7 Python, or risk messing up the installation of your macOS by installing clashing modules, just disregard my answer.

Leave a Comment