macos解决importerror:no module named cv2

问题描述:

macOS系统,使用brew install opencv后,通过python import cv2,提示”ImportError: No module named cv2”。

解决办法:

解决办法很简单,其实安装完opencv时就有提示,只是自己没注意到

运行以下两行代码,问题解决

1
2
mkdir -p /Users/luoxin/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/luoxin/Library/Python/2.7/lib/python/site-packages/homebrew.pth

安装opencv时,整体显示内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
brew install opencv
==> Installing opencv from homebrew/science
==> Downloading https://homebrew.bintray.com/bottles-science/opencv-2.4.13.2.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring opencv-2.4.13.2.sierra.bottle.1.tar.gz
==> Caveats
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/luoxin/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/luoxin/Library/Python/2.7/lib/python/site-packages/homebrew.pth