问题
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
回答1:
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!
来源:https://stackoverflow.com/questions/62786036/opencv-gives-an-error-in-the-jupyter-notebook-but-works-in-python-cli