ModulenotFoundError:No模块名为' Cv2' (仅在VENV的VSCODE中)
当我尝试安装 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
编辑:
这是问题的屏幕截图,
就不会出现问题
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ctrl + shift + p 打开命令调色板搜索并选择 python:选择解释器,然后选择正确的解释器,这将解决您的问题。
提示 :每次选择新解释器时,请创建 new终端激活环境。
就您的问题而言,您可以按照以下步骤操作:
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.
Tip: Every time you choose a new interpreter, please create a new terminal to activate the environment.
As far as your question is concerned, you can follow these steps:
pip install opencv-python
to install the packageAlso you can use
pip show opencv-python
to see where the package is installed.为我解决的问题是将以下内容添加到
.vscode/settings.json
配置文件更改
venv
到虚拟env文件夹的名称。What solved it for me was adding the following to the
.vscode/settings.json
config filechange
venv
to the name of your virtual env folder.我可能有同样的问题。尽管我在虚拟环境中,但PIP并未将OpenCV安装到特定的子文件夹中:
而是始终使用全局 /Python /site-ackages:
因此,我通过直接调用“ Virtual Env” Pip的使用来使用它:
((假设您的虚拟环境在.venv中,则可以更改;)
现在库已放入:
我可以导入CV2。
I got probably the same problem. Although I was inside the virtual environment, pip did not installed OpenCV into the particular sub-folder:
but always to the global /python/site-packages:
So I forced the usage of the "virtual env" pip by calling it directly:
(Assuming your virtual environment is in .venv, you may change that ;))
Now the library was place into:
And I could import cv2.