This Rep is only a notes for myself of installing opencv and python2/3 onto MacOS. I meet some problems during the installation so I believe it would be better for me to make a record about how did I solved thoes problems.
Macbook Pro 2018
Date: 2018-07-24
OS: MacOS High Sierra 10.13.6
Python2 version: 2.7.15
Python3 version: 3.7.0
opencv version: 3.4.2
Xcode version: 9.4.1
- Install OpenCV 3 on macOS with Homebrew (the easy way) by Adrian Rosebrock
- Install OpenCV 3 on MacOS by VAIBHAW SINGH CHANDEL
all code are terminal command
The installation is simple and straightforward. But the accepting license part was not.
The tutorial I went through told me to run to accepting license part after the installation is done.
sudo xcodebuild -license acceptBut I got an error msg as
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instanceI solved it by run the following command which update Xcode.
Remember to "SHUT DOWN" your Mac instead of "RESTART" to finish this update.
The update will automatically power up your Mac if you choose "SHOTDOWN" but will be canceled if you "RESTART"
softwareupdate --list
softwareupdate --install -aAfter the update, run
sudo xcodebuild -license acceptand accept the license again to finish this step.
This step did get me any error. Just use the following commands.
ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"
# update homebrew
brew update
# Add Homebrew path in PATH
echo "# Homebrew" >> ~/.bash_profile
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile
source ~/.bash_profileFor this version of Homebrew, "homebrew-science" has been Deprecated. So we no longer need to tap it.
For this version of Homebrew,
brew install pythonwill only install python 3. which is same as running
brew install python3So what we need to do here for installing both is to run
brew install python2 python3After that, you should have both python2 and 3 installed on your Mac.
You no longer need to link python by your self. Brew will finish the rest for you.
# Check whether Python using homebrew install correctly
which python2 # it should output /usr/local/bin/python2
which python3 # it should output /usr/local/bin/python3
# Check Python versions
python2 --version # it should output Python 2.7.15
python3 --version # it should output Python 3.7.0According to VAIBHAW SINGH CHANDEL and my personal experience, this version of Homebrew will
Install python2 at /usr/local/bin/python2
Install python3 at /usr/local/bin/python3
and python command will point to /usr/bin/python. This is the python distribution which comes with your OS and not installed by Homebrew.
This step now is much easier than before. Homebrew has been simplified a lot for this part. You dont need to care about options and bindings. Just run
brew install opencvAnd add OpenCV’s site-packages path to global site-packages by
#python2
echo /usr/local/opt/opencv/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
#python 3
echo /usr/local/opt/opencv/lib/python3.7/site-packages >> /usr/local/lib/python3.7/site-packages/opencv3.pthNote that there is one thing weird about opencv for python3 about the binding name. You could rename it by using
cd /usr/local/opt/opencv3/lib/python3.7/site-packages/
mv cv2.cpython-37m-darwin.so cv2.soTest if python/opencv installed successfully
python2
Python 2.7.15 (default, Jul 23 2018, 21:27:06)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.2'
>>> quit()$ python3
Python 3.7.0 (default, Jul 23 2018, 20:22:55)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.2'
>>> quit()follow the steps bellow, you should be able ti set up the virtual env successfully.
pip install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper"
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
source ~/.bash_profile
mkvirtualenv cv-py2 -p python2
mkvirtualenv cv-py3 -p python3I met a problem about setting up opencv for the virtual env. Which is if I run the link from the same "/usr/local/opt/opencv" folder. Only the python 3.7 virtual machine will have opencv successfully. What I did here is to use "/usr/local/opt/opencv@3".Which actually pointing to the same place as "/usr/local/opt/opencv" did.
cd ~/.virtualenvs/cv-py2/lib/python2.7/site-packages/
ln -s /usr/local/opt/opencv/lib/python2.7/site-packages/cv2.so cv2.so
cd ~/.virtualenvs/cv-py3/lib/python3.7/site-packages/
ln -s /usr/local/opt/opencv@3/lib/python3.7/site-packages/cv2.so cv2.soTest if virtual python/opencv installed successfully
workon cv-py2
python2
Python 2.7.15 (default, Jul 23 2018, 21:27:06)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.2'
>>> quit()
deactivate
workon cv-py3
python3
Python 3.7.0 (default, Jul 23 2018, 20:22:55)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.2'
>>> quit()
deactivate