问题
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
回答1:
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.
回答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
来源:https://stackoverflow.com/questions/41248940/unable-to-import-opencv-in-jupyter-notebook-but-able-to-import-in-command-line-o