Search code examples
pythonopencv

can't import cv2 in VS Code


i am trying to install and import cv2 with many ways but i can't i am using python 3.12.1 and all the posts in stack overflow to fix it but i can't fix

i try:

python -m pip install cv2

and:

pip install opencv-python
>>> import cv2
Traceback (most recent call last):
  File "c:\Users\Admin\Music\file\nhandienkhuonmat.py", line 1, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

Solution

  • I just tried running these on my machine:

    pip3 install opencv-python
    python3 (to open python interactive terminal)
    import cv2
    

    and everything works on my end.

    So a few things could be the issue for you.

    1. Did you open a new terminal after running pip install? If not, please do so.
    2. What version of python do you have installed? if it is python3, then try using 'pip3' instead of 'pip' and see it that makes a difference.