Installing OpenCV in MacOS High Sierra for python 3
When trying to install OpenCV in Mac OS High Sierra by following tutorials on the internet, most probably you’ll end up having errors related to permission denied while installation of virtualenv and virtualenvwrapper. For example, the following is the most common one that most people encounter.
IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/virtualenv.py'
Most probably installing any python library using pip might give similar kind of errors. First, let's try to understand why this kind of errors occurs in Mac OS but not on Linux. In Mac OS, there is a new security layer called System Integrity Protection. This will protect changing system files even from the root user. Therefore for the permission relates issues, though you use sudo with pip it might not work. The other thing is python is already installed in Mac OS and above error shows the system installed python location. Most of the time people try to install Open CV and python using brew and though they set path to use python installed by brew, the above error can occur. This might occur due to previously installed packages with system python installation and brew try to attach to those.
Following steps will help to resolve those issues.
- First of all install Xcode. This is to install C compiler to mac and easiest way to install Xcode is via the app store and you’ll able to find an ample amount of tutorials on how to do this on the internet. After that install Xcode command-line tools using the following command.
sudo xcode-select --install
2. Install homebrew using any tutorial and add the following line to at the end of your ~/.bash_profile or ~/.bashrc file. I prefer .bashrc since I use a custom terminal and bash_profile won’t always run.
export PATH=/usr/local/bin:$PATH
And then use the source command to load the changes in the corresponding file.
source ~/.bashrc
3. Then install python using brew using the following command and link them.
brew install python python3
brew link python
brew link python3
Note that we have installed both python2 and python3 as well in here it is better since using system python can cause problems in future.
4. Next, let's install pip3 for python3. Unlike with normal brew python 2.7 installation, it won’t automatically install with python3. You have to run the following command to do that.
brew postinstall python3
5. Then we have to install virtualenv and virtualenvwrapper. This is to create a virtual environment for each project therefor dependencies of multiple projects do not collide with each other. This comes handy when two projects need the same python package but with two different versions.
pip3 install virtualenv virtualenvwrapper
After that add following lines to the ~/.bashrc or ~/.bash_profile
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/Develsource /usr/local/bin/virtualenvwrapper.sh
6. Install OpenCV using following command. This will install OpenCV globally and later when we create virtual environments and we’ll refer this within our virtual environments since we do not need to install it in every environment we create.
brew install opencv
7. Add OpenCV site path to global site path. Please note that python version number can different in your installation and change accordingly if required.
echo /usr/local/opt/opencv/lib/python3.6/site-packages >> /usr/local/lib/python3.6/site-packages/opencv3.pth
8. Now let's create a virtual environment for the OpenCV project and point our previous OpenCV installation so that it can reuse it. To create a virtual environment with the name of cv-py3 run first command. You can give any name you prefer. The second command will switch to that env and the third one will exit from env.
mkvirtualenv cv-py3 -p python3
workon cv-py3
deactivate
After you run the second command it will switch to that environment and whatever python packages you install will be within the environment. This virtual environment is located in ~/.virtualenvs. You’ll see a separate folder for each environment. Now you can install any python library within this environment see the following example.
pip install numpy scipy scikit-image matplotlib scikit-learn
Then lets link previously installed OpenCV library to this virtual environment so we can use it without reinstalling.
cd ~/.virtualenvs/cv-py3/lib/python3.6/site-packages/ln -s /usr/local/opt/opencv@3/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so
9. Now let's try running OpenCV
The first command will switch to previously created env second will run the python3 and the third one will import open cv. If the third one runs without any errors, installation is a success. And you can print the version of it using the fourth command.
workon cv-py3
python3
import cv2
print(cv2.__version__)
You can exit from env using deactivate command.