Ask Your Question

Revision history [back]

ImportError: No module named 'cv2' in OSX for OpenCV3 using Python3.5.2

I have installed opencv3 with Python3 using this command:

brew install opencv3 --with-python3 --with-contrib

I get this:

bash-4.3$ brew info opencv3
homebrew/science/opencv3: stable 3.1.0 (bottled), HEAD [keg-only]
Open source computer vision library, version 3
http://opencv.org/
/usr/local/Cellar/opencv3/3.1.0_3 (479 files, 130.7M)
  Built from source on 2016-08-04 at 19:23:10 with: --with-python3 --with-contrib
From: https://github.com/Homebrew/homebrew-science/blob/master/opencv3.rb
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: jpeg ✔, libpng ✔, libtiff ✔
Recommended: eigen ✔, openexr ✔, homebrew/python/numpy ✔
Optional: ffmpeg ✔, gphoto2 ✘, gstreamer ✘, jasper ✘, libdc1394 ✘, openni ✘, openni2 ✘, qt ✔, qt5 ✘, tbb ✔, vtk ✔
==> Options
--32-bit
    Build 32-bit only
--c++11
    Build using C++11 mode
--with-contrib
    Build "extra" contributed modules
--with-cuda
    Build with CUDA v7.0+ support
--with-examples
    Install C and python examples (sources)
--with-ffmpeg
    Build with ffmpeg support
--with-gphoto2
    Build with gphoto2 support
--with-gstreamer
    Build with gstreamer support
--with-jasper
    Build with jasper support
--with-java
    Build with Java support
--with-libdc1394
    Build with libdc1394 support
--with-opengl
    Build with OpenGL support (must use --with-qt5)
--with-openni
    Build with openni support
--with-openni2
    Build with openni2 support
--with-python3
    Build with python3 support
--with-qt
    Build the Qt4 backend to HighGUI
--with-qt5
    Build the Qt5 backend to HighGUI
--with-quicktime
    Use QuickTime for Video I/O instead of QTKit
--with-static
    Build static libraries
--with-tbb
    Enable parallel code in OpenCV using Intel TBB
--with-vtk
    Build with vtk support
--without-eigen
    Build without eigen support
--without-numpy
    Use a numpy you've installed yourself instead of a Homebrew-packaged numpy
--without-opencl
    Disable GPU code in OpenCV using OpenCL
--without-openexr
    Build without openexr support
--without-python
    Build without Python support
--without-test
    Build without accuracy & performance tests
--HEAD
    Install HEAD version
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

opencv3 and opencv install many of the same files.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/opencv3/lib
    CPPFLAGS: -I/usr/local/opt/opencv3/include


If you need Python to find bindings for this keg-only formula, run:
  echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth

I have this version of python:

bash-4.3$ python
Python 3.5.2 (default, Jul 28 2016, 21:28:00)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.4/bin', '/Users/mona', '/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/site-packages', '/usr/local/Cellar/numpy/1.11.1/libexec/nose/lib/python3.5/site-packages']

Here's the gist log:

bash-4.3$ brew gist-logs opencv3
https://gist.github.com/9c8339b6da6b7771a78c0d0c0b7f3a0b

I am very confused on how to fix this problem. I am totally new to this and I am not an OSX expert anyways.

bash-4.3$ cat ~/.bashrc | grep python alias python='python3' bash-4.3$ cat ~/.bash_profile | grep python bash-4.3$ cat ~/.profile | grep python

If I could be guided as how to set the path or how to fix this situation step by step that would be great!

bash-4.3$ python
Python 3.5.2 (default, Jul 28 2016, 21:28:00)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2'

In the gist log I see that:

-- Found PythonInterp: /usr/bin/python (found suitable version "2.7.10", minimum required is "2.7") 
-- Found PythonLibs: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib (found suitable exact version "2.7.10") 
-- Found PythonInterp: /usr/local/bin/python3.4 (found suitable version "3.4.1", minimum required is "3.4") 
-- Could NOT find PythonLibs: Found unsuitable version "3.5.2", but required is exact version "3.4.1" (found /usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/config-3.5m/libpython3.5.dylib)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'numpy'

But when I uninstalled python3.5.2 using brew uninstall --force python3 and installed python3.4.1 from source on OSX I got the same error ImportError: No module named 'numpy' and when I went to Python3.4.1 interactive shell I couldn't import pip even though pip was installed. More detail of what was my problem with pip is documented here: http://stackoverflow.com/questions/38777010/importerror-no-module-named-numpy-for-python3-4-1-in-osx

However I have seen people who have had opencv3 running with python3.5.2, so I wonder how could this problem be fixed?

Also as I said, of course I have this:

bash-4.3$ python3.4
Python 3.4.1 (default, Aug  4 2016, 16:56:02)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'pip'
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2'
>>>

Somehow pip doesn't get installed for Python3.4.1 for me. bash-4.3$ pip -V pip 8.1.2 from /Library/Python/2.7/site-packages (python 2.7) bash-4.3$ pip3 -V pip 8.1.2 from /usr/local/lib/python3.5/site-packages (python 3.5)

I have even tried installing it from source:

curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
bash-4.3$ sudo python3.4 get-pip.py
Password:
Traceback (most recent call last):
  File "get-pip.py", line 19177, in <module>
    main()
  File "get-pip.py", line 194, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip
  File "/tmp/tmpfokbh13c/pip.zip/pip/__init__.py", line 16, in <module>
  File "/tmp/tmpfokbh13c/pip.zip/pip/vcs/subversion.py", line 9, in <module>
  File "/tmp/tmpfokbh13c/pip.zip/pip/index.py", line 30, in <module>
  File "/tmp/tmpfokbh13c/pip.zip/pip/wheel.py", line 39, in <module>
  File "/tmp/tmpfokbh13c/pip.zip/pip/_vendor/distlib/scripts.py", line 14, in <module>
  File "/tmp/tmpfokbh13c/pip.zip/pip/_vendor/distlib/compat.py", line 66, in <module>
ImportError: cannot import name 'HTTPSHandler'

For which I saw articles like https://support.mesosphere.com/hc/en-us/articles/204622579-I-m-getting-Python-PIP-errors-how-can-I-resolve-them- which suggest to install openssl (I already have it):

bash-4.3$ openssl version
OpenSSL 0.9.8zh 14 Jan 2016

So I am not sure how this problem can be fixed? Any idea is really appreciated.

Some of the things I have thought but don't know how to do them, so I am calling on experts:

  • install Python3.4.1 using brew (however when you install python3 using brew it installs python3.5.2 by default--If you know how to do this please let me know)
  • installing ImportError: cannot import name 'HTTPSHandler' module for Python3.4.1 so that I can install pip from its source code

Sorry this is a very confusing problem that I have put more than 3 complete days into figuring it but no solution.