Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import opencv in Jupyter notebook but able to import in command line on Anaconda

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

like image 301
footy Avatar asked Dec 20 '16 18:12

footy


1 Answers

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.

like image 107
footy Avatar answered Oct 23 '22 11:10

footy