1

So, I am using OpenCV in python. I installed opencv-python using pip install opencv-python. Whenever I try importing OpenCV in my terminal using the Python CLI (run python in command prompt and then run import cv2) it works perfectly fine, but when I try importing it in Jupyter Notebook/Jupyter Lab (also using import cv2), it gives the following error:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-c8ec22b3e787> in <module>
----> 1 import cv2

~\Anaconda3\lib\site-packages\cv2\__init__.py in <module>
      1 import importlib
      2 
----> 3 from .cv2 import *
      4 from .data import *
      5 

ModuleNotFoundError: No module named 'cv2.cv2'

I know that there is already a thread on this (found here) but I tried everything in that thread and nothing worked. I also saw this (I do have a pyd file in that directory) and this (nothing works) and even this (installing nb_conda and jupyter don't do anything). Please help!

EDIT: Here are some more things I tried:

  • conda install -c anaconda opencv - doesn't change anything
  • use a venv - already tried, doesn't change anything

EDIT 2: Looks like this issue is only with jupyter notebook whenever a library uses . to import something. For example, the error here occurs when opencv tries to import .cv2. I also have posted another question a few days ago here about stable-baselines not working in jupyter notebook, and the problem with that was that the module was trying to import from . import _ufuncs (another . import). Do others have this problem in Jupyter Notebook? Also, should I make a new StackOverflow post on . imports?

~ Ayush

Ayush Garg
  • 2,234
  • 2
  • 12
  • 28
  • 2
    You are using Anaconda. Have you tried doing `conda install -c anaconda opencv`? – rayryeng Jul 08 '20 at 01:44
  • @rayryeng Yes, I have, but it doesn't change anything... The same error occurs on jupyter notebook but not in Python CLI – Ayush Garg Jul 08 '20 at 02:28
  • How are you launching the notebook server? And when you launch the notebook file itself, are you picking the system Python or the Anaconda Python? – Mike Sandford Jul 08 '20 at 02:45
  • I use the command `jupyter notebook` in my cmd. – Ayush Garg Jul 08 '20 at 02:47
  • 1
    check the kernel on jupyter notebook, and remember to swap the venv before you launch `jupyter notebook` on cmd – CuCaRot Jul 08 '20 at 03:50
  • I am not using a `venv`, although I did try one and it didn't work. Also, yes, the kernel is in my main Python 3 env where I installed `opencv-python`. – Ayush Garg Jul 08 '20 at 16:45

1 Answers1

0

Yes! I got the answer!!

So, when I looked at __init__.py in the cv2 library, I found this line:

from .cv2 import *

So, I changed it to:

from cv2.cv2 import *

and everything works now! Hope this helps other people in the future!

Ayush Garg
  • 2,234
  • 2
  • 12
  • 28