Florian
Florian

Reputation: 43

import cv2 in python in vs code not working

The python code I wanted to run:

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. And currently I am running the verion 3.9.9

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

I installed opencv in the Command Prompt:

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

Upvotes: 1

Views: 21106

Answers (3)

JinSnow
JinSnow

Reputation: 1663

Try running pip3.10 install opencv-python.

Because:

  • cv2 is a part of opencv-python
  • and/or you might not have installed the module in the right python folder version.

Upvotes: 0

Tony Guizar
Tony Guizar

Reputation: 21

I had the same issue in VScode using python 3.9.6. I switched to python 3.10.7 and it worked fine.

Upvotes: 2

Reza Tanakizadeh
Reza Tanakizadeh

Reputation: 127

in your VScode press Ctrl+Shift+P and then type "Python: Select Interpreter" then select your python Interpreter and Run it again.

Upvotes: 3

Related Questions