ModuleNotFoundError: No module named 'cv2'
Asked Answered
F

5

15

I have been working on this error for a long time now. I have Python 3.6 and Python 2.7. I have tried to install opencv 2 and 3 in Python 2.7 and Python 3.6 respectively. I know the python interpreter I am using and I can interchange between them when I want.

When I run Python interpreter and write import cv2 it does import it. When I run the code from command prompt it says ModuleNotFoundError: No module named 'cv2'. The module is installed. The cv2.pyd file is in C:\Python27\Lib\site-packages I have attached a screen shot which shows the modules in Python27

enter image description here

I have used pip install opencv-python. I have downloaded the module from different sites and manually copy pasted it in the correct folder. Nothing works and I am seriously short of ideas now.

EDIT: I am on windows 10 with python 3.6 installed through anaconda and python 2.7 installed directly. Both have their variables set in the path

Ferdelance answered 20/10, 2017 at 17:27 Comment(6)
it doesn't work for python 2 or python 3, or both? have you tried passing the absolute path for pip (in scripts)Ey
I have downloaded the module from different sites and manually copy pasted it in the correct folder. What?? Why?? That's not how it is supposed to be.Domino
@Jean-FrançoisFabre works for python3 not for python 2. The code I am running only works for python 2.Ferdelance
@Domino I know!!! now you can understand how frustrated I am to do thisFerdelance
Simplest possible experiment: Using the same python that you can manually import cv2 from, if you have a one-line test.py that reads import cv2, does python test.py produce the import error?Drew
#54148422Watson
C
5

Faced with the same issue on Windows 10 I downloaded the open cv binary from the Unofficial Windows Binaries for Python Extension Packages.

Search the page for opencv and for and download the correct .whl for your system. Then pip install it. By example, on my system, after opening a cmd window I typed the following.

pip install opencv_python-3.4.1-cp36-cp36m-win_amd64.whl

I then opened python and the following worked

import cv2
print(cv2.__version__)

More info is available in this Mark Jay video.

:D

Carbohydrate answered 16/3, 2018 at 19:30 Comment(3)
Not working. I downloaded the latest .whl from the above mentioned link and installed it. When I did import cv2 it was still not able to find it. So added this line in the code to point to the installed location. Then I was able to import cv2 successfully but then when I tried to use imread parameter I got the error "AttributeError: module 'cv2' has no attribute 'imread'". Any idea where I am doing wrong ?Gersham
Does this answer help SO:a/47859122?Carbohydrate
OpenCV now builds official python packages. opencv-python is the canonical one. all of them contain the base modules, some contain contrib modules, some omit GUI functionality. install exactly one of them.Phoenician
D
21

In Windows 10 you can install it as

pip install opencv-python

this will allow you to import cv2 module

Deposal answered 31/10, 2019 at 13:5 Comment(0)
C
5

Faced with the same issue on Windows 10 I downloaded the open cv binary from the Unofficial Windows Binaries for Python Extension Packages.

Search the page for opencv and for and download the correct .whl for your system. Then pip install it. By example, on my system, after opening a cmd window I typed the following.

pip install opencv_python-3.4.1-cp36-cp36m-win_amd64.whl

I then opened python and the following worked

import cv2
print(cv2.__version__)

More info is available in this Mark Jay video.

:D

Carbohydrate answered 16/3, 2018 at 19:30 Comment(3)
Not working. I downloaded the latest .whl from the above mentioned link and installed it. When I did import cv2 it was still not able to find it. So added this line in the code to point to the installed location. Then I was able to import cv2 successfully but then when I tried to use imread parameter I got the error "AttributeError: module 'cv2' has no attribute 'imread'". Any idea where I am doing wrong ?Gersham
Does this answer help SO:a/47859122?Carbohydrate
OpenCV now builds official python packages. opencv-python is the canonical one. all of them contain the base modules, some contain contrib modules, some omit GUI functionality. install exactly one of them.Phoenician
P
2

@SpeedCoder5's solution applies only to Windows users. I am however on macOS (specifically macOS Monterey) and I managed to solve this issue by using the Jupyter Notebook extension in VS Code (Python 3.9) after I installed cv2 with

pip install opencv-python

screenshot of jupyter notebook oj vscode

Pinfish answered 12/3, 2022 at 10:33 Comment(1)
this is the same advice given by an answer from 2.5 years earlier (the other answer). pip install ... is supposed to work on all platforms, maybe with a pip3 instead.Phoenician
H
0

In my case I had a too old version of pip which was 10.0.1. I upgraded it with

py -3.7 -m pip install --upgrade pip to pip 23.2.1 (for my Python 3.7).

Then I ran py -3.7 -m pip install opencv-python again and it worked.

Hurdle answered 12/8, 2023 at 21:39 Comment(0)
S
0

This solution for VS code users. You might be here because non of the above solution work for you.

Go to bottom right corner of VS code : enter image description here

Click on it and select the correct path of the python installed.

Now you can import cv2, GOOD LUCK!

Schweiz answered 29/6 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.