Cannot find module cv2 when using OpenCV

Posted on

Solving problem is about exposing yourself to as many situations as possible like Cannot find module cv2 when using OpenCV and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Cannot find module cv2 when using OpenCV, which can be followed any time. Take easy to follow this discuss.

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.

Answer #1:

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
Answered By: Rika

Answer #2:

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.

Answered By: Midhun

Answer #3:

I solved my issue using the following command :

conda install opencv
Answered By: tarun kumar Sharma

Answer #4:

Try to add the following line in ~/.bashrc

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
Answered By: lanpa

Answer #5:

Python3.x User

sudo pip3 install opencv-python
Answered By: Anoop Kumar

Answer #6:

None of the above answers worked for me. I was going crazy until I found this solution below!

Simply run:

sudo apt install python-opencv
Answered By: Fatmajk

Answer #7:

For Windows 10 and Python 3.6, this worked for me

pip install opencv-contrib-python

Answered By: 4m01

Answer #8:

I solved my issue using the following command :

pip install opencv-python
Answered By: jincy mariam

Leave a Reply

Your email address will not be published. Required fields are marked *