简体   繁体   English

导入“cv2”无法解决

[英]Import "cv2" could not be resolved

I've installed python and opencv by using the correct commands in the cmd, but when I type in 'import cv2' in a python then I get the error in visual studio code and I can't find any solution online how to fix this I've installed python and opencv by using the correct commands in the cmd, but when I type in 'import cv2' in a python then I get the error in visual studio code and I can't find any solution online how to fix this

Import "cv2" could not be resolved Pylance (reportMissingImports)

More of the error in the terminal终端中的更多错误

ModuleNotFoundError: No module named 'cv2'

Make sure that its using the correct version of Python;确保它使用正确版本的 Python; eg, I installed Python from the windows store, which is working:例如,我从 windows 商店安装了 Python,它正在工作:

这个版本对我有用

However, if I use another one, it does not work:但是,如果我使用另一个,它不起作用:

在此处输入图像描述

In your terminal ( cmd if Windows) run the following (make sure that you're on the current working environment):在您的终端(如果是 Windows, cmd )运行以下命令(确保您处于当前工作环境中):

pip install opencv-python

then:然后:

pip install opencv-python-headless

I noticed that Visual Studio does not support the latest Python 3.8, so I had to use PyCharm as an IDE.我注意到 Visual Studio 不支持最新的 Python 3.8,所以我不得不使用 PyCharm 作为 IDE。

It just happened to me and I solved it installing both opencv-python and opencv-python-headless with pip and reloading the Visual Studio Code window right after it.它刚刚发生在我身上,我解决了它安装 opencv-python 和 opencv-python-headless 与 pip 并在它之后重新加载 Visual Studio Code window 。

To install the needed packages, just run this command in the terminal:要安装所需的软件包,只需在终端中运行以下命令:

$ pip install opencv-python opencv-python-headless

I had a similar issue so I installed opencv-python-headless (install opencv-python if not earlier) and reloaded the VScode window.我有一个类似的问题,所以我安装了 opencv-python-headless(如果不是更早安装 opencv-python)并重新加载了 VScode window。

pip install opencv-python-headless

However, some methods like imshow() kept failing:但是,像 imshow() 这样的一些方法一直失败:

cv2.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'

This was resolved by installing opencv-contrib-python.通过安装 opencv-contrib-python 解决了这个问题。 You can install it by running the following code in your terminal:您可以通过在终端中运行以下代码来安装它:

pip install opencv-contrib-python

I just uninstalled the older python version and installed a new one again.我刚刚卸载了旧的 python 版本并再次安装了新版本。 That is probably the simplest and the most hassle-free one in my mind.这可能是我心中最简单、最轻松的一个。 Stick to version 3.8.8, that should do the trick.坚持使用 3.8.8 版本,这应该可以解决问题。 Also no worries about maintaining multiple versions of python in the system.也不用担心在系统中维护多个版本的 python。 It is indeed true that the lack of support from VS is causing this issue.确实,缺乏 VS 的支持导致了这个问题。

I am on a mac and struggle with this and this is the best way to do it, when ur in your.py file, on the lower left part of your vscode windown u will see the version of python you are using, click that and try different versions until your open cv command works, in my case, 3.8.8 worked for me.我在 Mac 上为此苦苦挣扎,这是最好的方法,当你在你的 .py 文件中时,在你的 vscode 窗口的左下部分你会看到你正在使用的 python 的版本,点击它并尝试不同的版本,直到您的 open cv 命令起作用,就我而言,3.8.8 对我有用。

That is it, I had 4 versions of Python installed and removed all the older versions but 3.10.0 and voila - it works.就是这样,我安装了 4 个版本的 Python 并删除了所有旧版本,但 3.10.0 和瞧 - 它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM