简体   繁体   English

在 python 中导入 cv2 在 vs 代码中不起作用

[英]import cv2 in python in vs code not working

The python code I wanted to run:我要运行的 python 代码:

import cv2
print(cv2.__verion__)  

The Error code I am getting:我得到的错误代码:

Windows PowerShell
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Try the new cross-platform PowerShell https://aka.ms/pscore6
    
    PS D:\Programme\Visual Studio\New Projects> & C:/Users/Florian/AppData/Local/Programs/Python/Python310/python.exe "d:/Programme/Visual Studio/New Projects/Python/opencv_test.py"
    Traceback (most recent call last):
      File "d:\Programme\Visual Studio\New Projects\Python\opencv_test.py", line 1, in <module>
        import cv2
    ModuleNotFoundError: No module named 'cv2'

But if I type the exact same code in the Terminal it works:但是,如果我在终端中输入完全相同的代码,它就会工作:

    PS D:\Programme\Visual Studio\New Projects> python
    Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import cv2
    >>> print(cv2.__version__)
    4.5.4
    >>>

So far I installed python via Command Prompt and via Microsoft Store.到目前为止,我通过命令提示符和 Microsoft Store 安装了 python。 And currently I am running the verion 3.9.9目前我正在运行 verion 3.9.9

PS D:\Programme\Visual Studio\New Projects> python --version
Python 3.9.9

I installed opencv in the Command Prompt:我在命令提示符中安装了 opencv:

PS C:\Users\Florian> pip install opencv-python
Requirement already satisfied: opencv-python in c:\users\florian\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (4.5.4.60)
Requirement already satisfied: numpy>=1.19.3 in c:\users\florian\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from opencv-python) (1.21.5)
PS C:\Users\Florian>

I searched every Forum I could think of, but cant find a fix.我搜索了我能想到的每个论坛,但找不到解决方法。

Please help请帮忙

I had the same issue in VScode using python 3.9.6.我在使用 python 3.9.6 的 VScode 中遇到了同样的问题。 I switched to python 3.10.7 and it worked fine.我切换到 python 3.10.7 并且工作正常。

in your VScode press Ctrl+Shift+P and then type "Python: Select Interpreter" then select your python Interpreter and Run it again.在您的 VScode 中按 Ctrl+Shift+P,然后键入“Python: Select 解释器”然后 select 您的 python 解释器并再次运行它。

Try running pip3.10 install opencv-python .尝试运行pip3.10 install opencv-python

Because:因为:

  • cv2 is a part of opencv-python cv2 是 opencv-python 的一部分
  • and/or you might not have installed the module in the right python folder version.和/或您可能没有在正确的 python 文件夹版本中安装该模块。

I had the same problem and search anywhere for the answer.我有同样的问题,并在任何地方搜索答案。 selecting the right interpreter will solve this problem.选择正确的解释器将解决这个问题。 you might use vscode by the anaconda, but you should use the python interpreter where you installed it.您可以通过 anaconda 使用 vscode,但您应该使用安装它的 python 解释器。

sample: select interpreter示例:select 解释器

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

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