OpenCV在Jupyter Notebook中给出错误,但在Python CLI中有效 [英] OpenCV gives an error in the Jupyter Notebook but works in Python CLI

查看:325
本文介绍了OpenCV在Jupyter Notebook中给出错误,但在Python CLI中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在python中使用OpenCV.我使用pip install opencv-python安装了opencv-python.每当我尝试使用Python CLI在终端中导入OpenCV(在命令提示符下运行python然后运行import cv2)时,它都能正常工作,但是当我尝试在Jupyter Notebook/Jupyter Lab中导入(也使用import cv2 ),则出现以下错误:

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'

我知道已经有一个线程了(在此处找到),但是我尝试了该线程中的所有操作,但没有任何效果.我还看到了(我在该目录中确实有一个pyd文件)和(无效),甚至

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!

编辑:这是我尝试过的其他操作:

Here are some more things I tried:

  • conda install -c anaconda opencv-不更改任何内容
  • 使用venv-已经尝试过,不会更改任何内容
  • conda install -c anaconda opencv - doesn't change anything
  • use a venv - already tried, doesn't change anything

看起来只有库使用.导入某些内容时,jupyter notebook才会出现此问题.例如,当opencv尝试导入.cv2时,将发生此错误.几天前,我也在此处发布了有关stable-baselines而不是在jupyter notebook中工作,问题在于该模块试图导入from . import _ufuncs(另一个.导入). 其他人在Jupyter Notebook中有此问题吗?另外,我应该在.进口商品上发布新的StackOverflow帖子吗?

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

推荐答案

是的!我得到了答案!

因此,当我在cv2库中查看__init__.py时,发现以下行:

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

from .cv2 import *

因此,我将其更改为:

from cv2.cv2 import *

现在一切正常!希望以后对其他人有帮助!

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

这篇关于OpenCV在Jupyter Notebook中给出错误,但在Python CLI中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆