尚未解决的参考"cv2"内部cv2(cv2.cv2) [英] Unresolved reference "cv2" inside cv2 (cv2.cv2)

查看:426
本文介绍了尚未解决的参考"cv2"内部cv2(cv2.cv2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,人们似乎也遇到了类似的问题,但是没有一个人准确地描述了我的情况,对他们有用的解决方案似乎对我也不起作用(或者根本没有答案). /p>

在安装opencv-pythonopencv-contrib-python后使用pycharm,我注意到import cv2可以工作,但是当我尝试访问cv2.imread()时,pycharm抱怨找不到它.

所以我转到了cv2初始化文件,它看起来像这样:

import importlib

from .cv2 import *
from .data import *

# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)

Pycharm在from .cv2 import *行上检测到未解析的引用,并且我想象在最后一行上会发生相同的问题-我尝试在python控制台中执行以下操作:

import cv2
print(__version__)

但是我遇到了NameError,这似乎证实了我的怀疑.

正如我写的那样,我曾尝试安装opencv-contrib-python,但似乎没有任何作用,坦率地说,我已经没有主意了.

注意: -我使用的是Windows 10 x64. -我使用的是Python 3.6 x64. -我在Pycharm项目上设置了虚拟环境.

解决方案

我发现问题出在cv2的__init__.py如何导入所有内容,即globals().update()行.

从本质上讲,一切正常,但是由于Pycharm不会索引这种调用,因此它不会检测到名称空间已正确导入.因此,解决方案将是忽略错误",并且不进行自动补全.

I've looked around and people seem to have similar problems but none described my case exactly, and solutions that worked for them didn't seem to work for me (or there was no answer to the question at all).

Using pycharm, after having installed opencv-python and opencv-contrib-python I noticed that import cv2 works, but when I tried accessing cv2.imread() pycharm complained about not being able to find it.

So I went to the cv2 init file, which looks like this:

import importlib

from .cv2 import *
from .data import *

# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)

Pycharm detects an unresolved reference on the from .cv2 import * line and I imagine the same problem happens on the last line - I tried doing the following in a python console:

import cv2
print(__version__)

But I got a NameError, which seems to confirm my suspicion.

As I wrote, I have tried installing opencv-contrib-python but that didn't seem to do anything and frankly I'm already out of ideas.

Notes: - I'm on Windows 10 x64. - I'm using Python 3.6 x64. - I have a virtual environment set up on my Pycharm project.

解决方案

I found out that the problem is due to how cv2's __init__.py imports all the stuff, namely the globals().update() line.

In substance, everything does work, but since Pycharm doesn't index that kind of call, it doesn't detect the namespace as being properly imported. The solution would thus be to ignore the "error" and do without autocompletion.

这篇关于尚未解决的参考"cv2"内部cv2(cv2.cv2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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