2017-06-20 175 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 

後,無論進口甲基

相關問題