5

I created a new environment in anaconda for python 3.5 and installed all the required pip libraries including opencv.

If I execute the following in command line

$ python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 

As you can see above there is no issues importing cv2.

However when I open Jupyter notebook and execute the following

#importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
%matplotlib inline

I get the following error

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)


<ipython-input-5-69f36577ffd4> in <module>()
      3 import matplotlib.image as mpimg
      4 import numpy as np
----> 5 import cv2
      6 get_ipython().magic('matplotlib inline')

ImportError: No module named 'cv2'

I also tried cycling through all the available kernals in Kernal->Change Kernal settings. That didnt help either

footy
  • 5,803
  • 13
  • 48
  • 96
  • import cv2 print cv2.__file__ should return path of that file. For example, I got /usr/local/lib/python2.7/site-packages/cv2.so in my system – Destrif Dec 20 '16 at 18:30
  • I got '/Users/abadarinath/Applications/anaconda/envs/UdacityNanoCar/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so' – footy Dec 20 '16 at 18:31
  • Can you do it in both case and compare the path? – Destrif Dec 20 '16 at 18:34
  • 1
    import sys sys.path.append('/Users/abadarinath/Applications/anaconda/envs/UdacityNanoCa‌​r/lib/python3.5/site‌​-packages') in your file – Destrif Dec 20 '16 at 18:35
  • From http://stackoverflow.com/questions/19876079/opencv-cannot-find-module-cv2 – Destrif Dec 20 '16 at 18:36
  • @Destrif it worked. thanks! I wonder why anaconda didnt find the package. – footy Dec 20 '16 at 18:41

2 Answers2

5

The following comment by @Destrif fixed it

import sys
sys.path.append('/Users/[username]/Applications/anaconda/envs/UdacityNanoCar/lib/python3.5/site-packages')

If there is more elegant answer I welcome it.

footy
  • 5,803
  • 13
  • 48
  • 96
  • If you need more informations, read this: http://stackoverflow.com/questions/897792/where-is-pythons-sys-path-initialized-from/38403654#38403654 – Destrif Dec 21 '16 at 01:31
  • For those who are lost here: Basically you're looking for the directory called _"site-packages"_ under your environment ("UdacityNanoCar" in this case). For me my environment, "*tensorflow*" was here: `C:\Users\[USERNAME]\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages`. My Anaconda was downloaded along with Visual Studio Enterprise 2017. – Anonymous Person Jul 15 '18 at 15:13
2

I also had this problem. I installed opencv-contrib-python package using pip in my conda env and getting the same error

$ source activate env 
$ pip install opencv-contrib-python

uninstalled opencv from my env and installed when I was not inside env i.e

$ source deactivate (if you are in the env )
$ pip install opencv-contrib-python
iamtodor
  • 734
  • 8
  • 21
Kartik
  • 240
  • 2
  • 13
  • so this fixed the issue for you? – Jon Dec 07 '17 at 21:50
  • `pip installed opencv-contrib-python` causes error. For that people who aren't famaliar with pip here is correct version: `pip install opencv-contrib-python` – iamtodor Oct 06 '18 at 12:59