ModulenotFoundError:No模块名为' Cv2' (仅在VENV的VSCODE中)

发布于 2025-02-13 18:07:07 字数 1005 浏览 0 评论 0原文

当我尝试安装 opencv-python 时,我会收到以下错误:

modulenotfounderror:no模块名为'cv2'

我尝试通过这些命令安装CV2:

pip pip install opencv-python

pip安装opencv-contrib-python

pip pip install opencv-python OPENCV-PYTHON-HEADLESS

Python3 -M PIP install opencv-python

pip3 install opencv-python-upgrade

但是,问题仍然存在。

我注意到这个问题仅发生在VSCODE的虚拟环境(VENV)中。

实际上,如果我从终端启动此代码,它不会给我任何错误,它将返回我的版本cv2

import cv2
print (cv2.__version__)

回复 - > 4.6.0

i使用python 3.9

编辑:

这是问题的屏幕截图,

就不会出现问题

如果我创建一个新的venv “在此处输入图像说明”

When I try to install opencv-python I get the following error:

ModuleNotFoundError: No module named 'cv2'

I tried to install cv2 via these commands:

pip install opencv-python

pip install opencv-contrib-python

pip install opencv-python opencv-python-headless

python3 -m pip install opencv-python

pip3 install opencv-python --upgrade

However, the problem remains.

I have noticed that this problem occurs only in the virtual environment (venv) of VScode.

In fact, if I launch this code from the terminal it does not give me any error and it returns me the version of cv2

import cv2
print (cv2.__version__)

Reply -> 4.6.0

I use Python 3.9

EDIT:

This is a screenshot of the problem

The problem do not appear if I create a new venv

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪意 2025-02-20 18:07:07

使用 ctrl + shift + p 打开命令调色板搜索并选择 python:选择解释器,然后选择正确的解释器,这将解决您的问题。

提示 :每次选择新解释器时,请创建 new终端激活环境。

就您的问题而言,您可以按照以下步骤操作:

  1. 在虚拟环境中选择解释器根据上述方法,
  2. 创建一个新的终端
  3. 使用pip install opencv-python以安装软件包,

您也可以使用pip show opencv-python查看安装软件包的位置。

Use Ctrl+Shift+P to open the command palette search and select Python:Select Interpreter, then select the correct interpreter, this will solve your problem.

enter image description here

Tip: Every time you choose a new interpreter, please create a new terminal to activate the environment.

enter image description here

As far as your question is concerned, you can follow these steps:

  1. Select the interpreter in the virtual environment according to the above method
  2. Create a new terminal
  3. Use pip install opencv-python to install the package

Also you can use pip show opencv-python to see where the package is installed.

enter image description here

世界和平 2025-02-20 18:07:07

为我解决的问题是将以下内容添加到.vscode/settings.json配置文件

    // Add venv to python path
    "python.analysis.extraPaths": [
        "${workspaceFolder}/venv/lib/python3.8/site-packages"
    ]

更改venv到虚拟env文件夹的名称。

What solved it for me was adding the following to the .vscode/settings.json config file

    // Add venv to python path
    "python.analysis.extraPaths": [
        "${workspaceFolder}/venv/lib/python3.8/site-packages"
    ]

change venv to the name of your virtual env folder.

沉溺在你眼里的海 2025-02-20 18:07:07

我可能有同样的问题。尽管我在虚拟环境中,但PIP并未将OpenCV安装到特定的子文件夹中:

.venv/lib/python3.11/site-packages/

而是始终使用全局 /Python /site-ackages:

/usr/local/lib/python3.11/site-packages

因此,我通过直接调用“ Virtual Env” Pip的使用来使用它:

.venv/bin/python3.11 -m pip install opencv-python

((假设您的虚拟环境在.venv中,则可以更改;)

现在库已放入:

.venv/lib/python3.11/site-packages/

我可以导入CV2。

I got probably the same problem. Although I was inside the virtual environment, pip did not installed OpenCV into the particular sub-folder:

.venv/lib/python3.11/site-packages/

but always to the global /python/site-packages:

/usr/local/lib/python3.11/site-packages

So I forced the usage of the "virtual env" pip by calling it directly:

.venv/bin/python3.11 -m pip install opencv-python

(Assuming your virtual environment is in .venv, you may change that ;))

Now the library was place into:

.venv/lib/python3.11/site-packages/

And I could import cv2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文