Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module cv2 when using OpenCV

I have installed OpenCV on the Occidentalis operating system (a variant of Raspbian) on a Raspberry Pi, using jayrambhia's script found here. It installed version 2.4.5.

When I try import cv2 in a Python program, I get the following message:

pi@raspberrypi~$ python cam.py Traceback (most recent call last) File "cam.py", line 1, in <module>     import cv2 ImportError: No module named cv2 

The file cv2.so is stored in /usr/local/lib/python2.7/site-packages/...

There are also folders in /usr/local/lib called python3.2 and python2.6, which could be a problem but I'm not sure.

Is this a path error perhaps? Any help is appreciated, I am new to Linux.

like image 941
user2971844 Avatar asked Nov 09 '13 12:11

user2971844


People also ask

How do I fix No module named cv2?

The Python "ModuleNotFoundError: No module named 'cv2'" occurs when we forget to install the opencv-python module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install opencv-python command.

Why is cv2 not working in python?

importerror no module named cv2 error occurs when cv2 module is not properly installed or its path is not properly set or configured. The straight way fix for this error (no module named cv2) is to reinstall this module (OpenCV-python). In some scenario reinstalling this module automatically remove the older version.


2 Answers

First do run these commands inside Terminal/CMD:

conda update anaconda-navigator   conda update navigator-updater   

Then the issue for the instruction below will be resolved

For windows if you have anaconda installed, you can simply do

pip install opencv-python 

or

conda install -c https://conda.binstar.org/menpo opencv 

if you are on linux you can do :

pip install opencv-python 

or

conda install opencv  

Link1 Link2

For python3.5+ check these links : Link3 , Link4

Update:
if you use anaconda, you may simply use this as well (and hence don't need to add menpo channel):

conda install -c conda-forge opencv 
like image 163
Hossein Avatar answered Sep 23 '22 01:09

Hossein


This happens when python cannot refer to your default site-packages folder where you have kept the required python files or libraries

Add these lines in the code:

import sys  sys.path.append('/usr/local/lib/python2.7/site-packages') 

or before running the python command in bash move to /usr/local/lib/python2.7/site-packages directory. This is a work around if you don't want to add any thing to the code.

like image 25
Midhun Avatar answered Sep 26 '22 01:09

Midhun