2017-06-20 148 views
0

我刚刚开始使用pycharm,python和opencv,并试图设置我的环境。我已经安装了所有必需的包和我导入的OpenCV像这样:PyCharm Opencv - 导入cv2.cv2的自动完成,导入cv2没有错误

import cv2 

然而,这并不能自动完成,并显示警告,调用的时候,方法可能会丢失,但如果我导入像这样:

import cv2.cv2 

自动完成确实工作,但运行产生以下错误:

Traceback (most recent call last): 
    File "C:/Users/dunnj/PycharmProjects/TransformApps/transformapps/blackwhite.py", line 1, in <module> 
    import cv2.cv2 as cv2 
AttributeError: 'module' object has no attribute 'cv2' 

任何帮助表示赞赏!

+2

尝试在站点包看看,并期待在CV2的位置,你会找到答案。 –

+0

@ArpitSolanki恐怕我不明白... cv2.pyd位于site-packages/cv2;它的'__init __。py'与matplotlib处于同一级别,但是matplotlib自动完成... cv2.py似乎是一个pycharm已经反编译的文件...如果是这样的话,我怎样才能让pycharm自动完成无论如何? –

+0

opencv在python绑定上运行并且遵循cpp源代码,并且它们可能无法在pycharm中正确自动完成 –

回答

4

贷记到ingolemor/learnpython。我被困在这里已经很久了,这让我很生气,所以我在这里分享。

我的OpenCV是通过使用包装opencv-python package


The sys.modules hacking that that module is doing is the source of the problem. Pycharm doesn't exactly import modules in order to know what's inside of them, so messing with the imports dynamically like that confuses pycharm greatly. It's not pycharm's fault, the opencv-python maintainer should have used a star import rather than that messy import hack. You should be able to work around the problem using the technique you stumbled upon. All you have to do is catch and ignore the error under normal operation:

import cv2 

# this is just to unconfuse pycharm 
try: 
    from cv2 import cv2 
except ImportError: 
    pass 
+0

最后我找到了这个工作解决方案 – liyuanhe211

0

我有同样的问题安装。
我用

import cv2 as cv2 

后,无论进口甲基