天天看點

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

openCV系列文章目錄

文章目錄

  • openCV系列文章目錄
  • 前言
  • 一、錯誤原因
  • 二、解決方法
    • 1.在vscode:Python:Select Interpreter
    • 2.依然報錯:cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
    • 3.再次報錯:WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000028558508F40>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/f0/cc/3b7408b8ecf7c1d20ad480c3eaed7619857bf1054b690226e906fdf14258/ffmpeg-1.4.tar.gz
    • 4.在vscode終端重裝opencv-python
    • 5.代碼
    • 6.運作

前言

在vscode中使用opencv-python報錯:

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法
import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

一、錯誤原因

1.首先

numpy matplotlib opencv_python這些我已經在本機配置安裝

pip install numpy matplotlib opencv_python

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

二、解決方法

1.在vscode:Python:Select Interpreter

當python環境不止一個時,vscode可以選擇指定的python解釋器,具體為:

vscode設定中打開Command Palette,ctrl+shift+p

輸入Python:Select Interpreter

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

根據自己機器的python.exe 選擇

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

這樣vscode下能夠識别cv2

2.依然報錯:cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvShowImage’

解決方法:在vscode終端輸入 :pip install ffmpeg

3.再次報錯:WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x0000028558508F40>, ‘Connection to files.pythonhosted.org timed out. (connect timeout=15)’)’: /packages/f0/cc/3b7408b8ecf7c1d20ad480c3eaed7619857bf1054b690226e906fdf14258/ffmpeg-1.4.tar.gz

解決:解除安裝opencv-python

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法
import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

4.在vscode終端重裝opencv-python

pip install opencv-python

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

5.代碼

import cv2


img = cv2.imread('E:/openCVExercise/pythonOpenCV/1.jpg')
cv2.imshow('img', img)
cv2.waitKey(0)
           
import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法

6.運作

import “cv2“ could not be resolved pylance(reportMissingImports)openCV系列文章目錄前言一、錯誤原因二、解決方法