Home > front end >  Installed opencv-python but cannot import (ModuleNotFoundError: No module named 'cv2')
Installed opencv-python but cannot import (ModuleNotFoundError: No module named 'cv2')

Time:06-08

I was installed all the opencv modules:

(venv) D:\#6>pip install opencv-python-headless
Collecting opencv-python-headless
  Downloading opencv_python_headless-4.5.5.64-cp36-abi3-win_amd64.whl (35.3 MB)
     ---------------------------------------- 35.3/35.3 MB 9.2 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17.3 in d:\venv\lib\site-packages (from opencv-python-headless) (1.22.4)
Installing collected packages: opencv-python-headless
Successfully installed opencv-python-headless-4.5.5.64
(venv) D:\#6>pip install opencv-contrib-python-headless
Collecting opencv-contrib-python-headless
  Downloading opencv_contrib_python_headless-4.5.5.64-cp36-abi3-win_amd64.whl (42.1 MB)
     ---------------------------------------- 42.1/42.1 MB 7.5 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.14.5 in d:\venv\lib\site-packages (from opencv-contrib-python-headless) (1.22.4)
Installing collected packages: opencv-contrib-python-headless
Successfully installed opencv-contrib-python-headless-4.5.5.64
(venv) D:\#6>pip install opencv-python
Requirement already satisfied: opencv-python in d:\venv\lib\site-packages (4.5.5.64)
Requirement already satisfied: numpy>=1.14.5 in d:\venv\lib\site-packages (from opencv-python) (1.22.4)
(venv) D:\#6>pip install opencv-contrib-python
Requirement already satisfied: opencv-contrib-python in d:\venv\lib\site-packages (4.5.5.64)
Requirement already satisfied: numpy>=1.17.3 in d:\venv\lib\site-packages (from opencv-contrib-python) (1.22.4)

My pip list:

Package                        Version
------------------------------ --------
numpy                          1.22.4
opencv-contrib-python          4.5.5.64
opencv-contrib-python-headless 4.5.5.64
opencv-python                  4.5.5.64
opencv-python-headless         4.5.5.64
Pillow                         9.1.1
pip                            22.1.2
PyYAML                         6.0
setuptools                     58.1.0

When I import cv2 to the code, it show me an error:

(venv) D:\#6>py
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

So how can I fix this? Help me please.
Thank you =)

CodePudding user response:

Use "python.exe -m pip install <module>" instead of "pip install <module>"

CodePudding user response:

You installed conflicting modules. Remove everything, install one of them.

opencv-contrib-python          4.5.5.64
opencv-contrib-python-headless 4.5.5.64
opencv-python                  4.5.5.64
opencv-python-headless         4.5.5.64

You must never have more than one of them at any time.

All of them contain the basic OpenCV modules.

The -headless variants omit highgui.

The -contrib variants add contrib modules but are not addons. They are complete packages that contain all the basic modules.

  • Related