431

I have installed OpenCV on the Occidentalis operating system (a variant of Raspbian) on a Raspberry Pi, using this script by jayrambhia. 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, in case that is relevant.

How can I fix the problem? Is this caused by a path misconfiguration?

4
  • 3
    try : python --version . if it says 3.2, then you got a hint. try to locate the 2.7 executable, and call that with an absolute path
    – berak
    Nov 9, 2013 at 12:49
  • 4
    For macOS 10.12 and python 3 the simple command below worked to resolve the error: pip install opencv-python Refer: pypi.python.org/pypi/opencv-python
    – Tushar
    Feb 20, 2017 at 4:42
  • If you are using macOS Catalina, maybe this can help you stackoverflow.com/a/61578366/11559619 May 3, 2020 at 16:59
  • 1
    pip install opencv-python==4.6.0.66 Jul 27, 2022 at 8:34

25 Answers 25

537

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
12
  • 9
    for linux you should type 'pip install opencv-python' or 'sudo pip install opencv-python'
    – Oz Radiano
    Apr 15, 2017 at 8:50
  • 25
    You got the command wrong. It is sudo pip3 install opencv-python. Also, for linux, you must run it as sudo pip3 install opencv-python for python 3 and sudo pip install opencv-python for python 2. You do not need the sudo at the beginning of the command if you do sudo -i forst or are running as root in some manner.
    – E.T.
    May 21, 2017 at 18:06
  • 2
    If facing problems while installing, make sure you have the newest version of pip pip install --upgrade pip this was my wattle
    – mrk
    Jun 20, 2017 at 12:30
  • 4
    pip install opencv-python works for me and I don't have Anaconda. Just FYI for anyone else who comes here.
    – user6096242
    Jul 22, 2017 at 15:09
  • 4
95

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.

7
  • 1
    Are you working on windows?This path look like at linux system. Apr 6, 2016 at 16:23
  • 4
    the question was about Raspbian OS , which is Linux .
    – Midhun
    Apr 6, 2016 at 20:33
  • 3
    Worked for me on mac machine
    – sandy
    Jul 19, 2016 at 9:14
  • it works for one session, but if I exit and re-launch python, I have to do it again. how to save this modification?
    – Mostafa
    Nov 3, 2016 at 15:26
  • I get "ImportError: numpy.core.multiarray failed to import" when I do this. Any suggestions?
    – hiquetj
    Feb 6, 2017 at 19:11
64

I solved my issue using the following command :

conda install opencv
4
  • 12
    If you've got Anaconda Python installed, this is the command you're looking for.
    – kevinmicke
    Jun 28, 2016 at 22:35
  • I tried this today on a Windows 64-bit 2.7 conda environment and got a PackageNotFound error
    – Mike Wise
    Dec 13, 2016 at 17:19
  • 2
    What did work was conda install -c https://conda.binstar.org/menpo opencv
    – Mike Wise
    Dec 13, 2016 at 17:20
  • pip install opencv-python==4.6.0.66 Jul 27, 2022 at 8:34
47

Try to add the following line in ~/.bashrc

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
1
  • 2
    Thanks, it worked for me when used: export PYTHONPATH=/usr/local/python/2.7:$PYTHONPATH
    – Ikbel
    Sep 13, 2015 at 13:38
38

Python3.x User

sudo pip3 install opencv-python
2
37

For Windows 10 and Python 3.6, this worked for me

pip install opencv-contrib-python

2
  • 1
    Worked for me on Linux (Ubuntu 16.04) too
    – FlorianH
    Mar 13, 2020 at 9:01
  • 1
    This solved my problem on Ubuntu as well.
    – roschach
    Jul 17, 2021 at 17:20
28

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
2
  • 1
    sudo apt install python3-opencv for Ubuntu 20 and probably others in future Aug 16, 2020 at 11:21
  • 2
    E: Unable to locate package python-opencv showing this error Nov 9, 2021 at 7:39
24
pip install opencv-python

or

pip install opencv-python3 

will definitely work fine.

23

I solved my issue using the following command :

pip install opencv-python
16

I solve it by running:

pip3 install opencv-python
11

I solve this by run code pip install opencv_contrib_python

9

Easy

python -m pip install --upgrade pip

Then

pip install opencv-python
8

I have come accross same as this problem i installed cv2 by

pip install cv2

However when i import cv2 module it displayed no module named cv2 error.
Then i searched and find cv2.pyd files in my computer and i copy and paste to site-packages directory

C:\Python27\Lib\site-packages

then i closed and reopened existing application, it worked.

EDIT I will tell how to install cv2 correctly.

1. Firstly install numpy on your computer by

pip install numpy


2. Download opencv from internet (almost 266 mb).
I download opencv-2.4.12.exe for python 2.7. Then install this opencv-2.4.12.exe file.
I extracted to C:\Users\harun\Downloads to this folder.
After installation go look for cv2.py into the folders.
For me

C:\Users\harun\Downloads\opencv\build\python\2.7\x64

in this folder take thecv2.pyd and copy it in to the

C:\Python27\Lib\site-packages

now you can able to use cv2 in you python scripts.

4
  • I don't think the package cv2 installed through piphas anything to do with openCV, you confirm ?
    – epsilones
    May 9, 2016 at 13:41
  • @Newben sorry for late response. in EDIT part i talk about how to install opencv correctly. i wish it will help you if you have not solve the problem May 9, 2016 at 18:27
  • Using pip install opencv-python should work when trying to install via pip. Jul 10, 2021 at 5:09
  • ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none) ERROR: No matching distribution found for cv2 Nov 9, 2021 at 7:44
6

IF YOU ARE BUILDING FROM SCRATCH, GO THROUGH THIS

You get No module named cv2.cv. Son, you did all step right, since your sudo make install gave no errors.

However look at this step

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

THE VERY IMPORTANT STEP OF ALL THESE IS TO LINK IT.

ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so 
or 
ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so

The moment you choose wise linking, or by brute force just find the cv2.so file if that exist or not

Here I am throwing my output.

    Successfully installed numpy-1.15.3
(cv) demonLover-desktop:~$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ pip list
Package    Version
---------- -------
numpy      1.15.3 
pip        18.1   
setuptools 40.5.0 
wheel      0.32.2 
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>> 
[2]+  Stopped                 python
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/c
ls: cannot access '/usr/local/lib/python2.7/site-packages/c': No such file or directory
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ deactivate 
demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/
demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls
cv2.so  easy_install.py  easy_install.pyc  numpy  numpy-1.15.3.dist-info  pip  pip-18.1.dist-info  pkg_resources  setuptools  setuptools-40.5.0.dist-info  wheel  wheel-0.32.2.dist-info
demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls /usr/local/lib/python2.7/site-packages/
demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls -l  /usr/local/lib/python2.7/site-packages/
total 0
demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls
cv2.so  easy_install.py  easy_install.pyc  numpy  numpy-1.15.3.dist-info  pip  pip-18.1.dist-info  pkg_resources  setuptools  setuptools-40.5.0.dist-info  wheel  wheel-0.32.2.dist-info
demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ workon cv
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>> 
[3]+  Stopped                 python
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ find / -name "cv2.so"
find: ‘/lost+found’: Permission denied
find: ‘/run/udisks2’: Permission denied
find: ‘/run/docker’: Permission denied
find: ‘/run/exim4’: Permission denied
find: ‘/run/lightdm’: Permission denied
find: ‘/run/cups/certs’: Permission denied
find: ‘/run/sudo’: Permission denied
find: ‘/run/samba/ncalrpc/np’: Permission denied
find: ‘/run/postgresql/9.5-main.pg_stat_tmp’: Permission denied
find: ‘/run/postgresql/10-main.pg_stat_tmp’: Permission denied
find: ‘/run/lvm’: Permission denied
find: ‘/run/systemd/inaccessible’: Permission denied
find: ‘/run/lock/lvm’: Permission denied
find: ‘/root’: Permission denied
^C
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ sudofind / -name "cv2.so"
sudofind: command not found
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ^C
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ sudo find / -name "cv2.so"
[sudo] password for app: 
find: ‘/run/user/1000/gvfs’: Permission denied
^C
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ sudo find /usr/ -name "cv2.so"
/usr/local/lib/python2.7/dist-packages/cv2.so
^C
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/dist-packages/ccv2.so cv2.so
click/                        clonevirtualenv.pyc           configparser-3.5.0.dist-info/ configparser.py               cv2.so                        cycler.py
clonevirtualenv.py            concurrent/                   configparser-3.5.0-nspkg.pth  configparser.pyc              cycler-0.10.0.dist-info/      cycler.pyc
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
ln: failed to create symbolic link 'cv2.so': File exists
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ rm cv2.so 
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ ls
cv2.so  easy_install.py  easy_install.pyc  numpy  numpy-1.15.3.dist-info  pip  pip-18.1.dist-info  pkg_resources  setuptools  setuptools-40.5.0.dist-info  wheel  wheel-0.32.2.dist-info
(cv) demonLover-desktop:~/.virtualenvs/cv/lib/python2.7/site-packages$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 

My step will only help, if your built is done right.

6
  • Only with the help of @TheExorcist could solve this May 28, 2019 at 19:43
  • Glad to help you @RodneySalcedo , but it is sin while linking, we commit unconsciously. May 29, 2019 at 12:55
  • Thanks, but why doesn't this happen automatically?
    – Cypher
    Mar 1 at 16:40
  • @Cypher nothing happens automatically, that's why exorcist exists Mar 2 at 8:09
  • 1
    @Cypher yes you can do that but you need to put a constraint of version of library else cmake build gonna give you error depending on sys arch, mostly building should be secondary pref. Mar 4 at 6:05
6

For python3x users

$ sudo pip3 install opencv-python

For python2x users

$ sudo pip install opencv-python
1
  • This answer didn't work for me on raspbian, it installed fine but didn't solve the problem.
    – user230910
    Mar 31, 2022 at 6:45
5

If want to install opencv in virtual environment. Run command in terminal for getting virtual environment list.

conda env list

or jupyter notebook command is

!conda env list

Then update your anaconda.

conda update anaconda-navigator
conda update navigator-updater

Install opencv in your selected environment path.

conda install -c ['environment path'] opencv

Juypter notebook

!conda install --yes --prefix ['environment path'] opencv
2

Create a virtual environment and then install open-cv there. It worked for me. And, it is best practice to create a virtual environment. Create a virtual environment:

python3 -m venv env

Activating virtual environment For Linux:

source env/bin/activate

Install OpenCV and other modules now.

pip3 install opencv-python

Above will work for linux. Similarly, you can create virtual environment for windows and then install opencv More on Virtual Environment:

0

Try this out:

sudo ldconfig

sudo nano /etc/ld.so.conf.d/opencv.conf

and add this following line in the opencv.conf not in the command window

/usr/local/lib

Then:

sudo ldconfig

sudo nano /etc/bash.bashrc

and add this two lines in the bash.bashrc not in the command window

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig       
export PKG_CONFIG_PATH

at last reboot your Pi sudo reboot now

and try import cv2

1
  • Can you please explain why these steps? How they are related to cv2? Jan 31, 2020 at 4:58
0

Another way I got opencv to install and work was inside visual studio 2017 community. Visual studio has a nice python environment with debugging.
So from the vs python env window I searched and added opencv. Just thought I would share because I like to try things different ways and on different computers.

VisualStudio2017 python environments with opencv

0

I had the same problem, just couldn't figure it out with opencv2 and opencv3 installed into /opt/opencv and opencv3 respectively. Turned out that bloody anaconda install of opencv in my home directory was first on path and mangled opencv. Removed it and started using /opt/opencv3/lib as defined in /etc/ld.so.conf.d/opencv.conf. Worked perfectly first go. Do you have anaconda installed? Could be the issue.

1
  • 1
    Can you tell me how you did it?
    – JaySabir
    Aug 20, 2020 at 3:58
0

I know you had the issue with Occidentalis, but I had the issue on Windows too, and I didn't found anything helpful. So if you have windows, and you've installed opencv, but you still get an Import error, this method will maybe solve the issue.

I managed to solve the issue, by reactivating anaconda.

First I deleted your\path\to\anaconda3 and your\path\to\anaconda3\Scripts from PATH (you will need these later, so remember these paths)

Then if you run python from cmd it will say:

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation.

Then in Anaconda prompt I ran these:

your\path\to\anconda3\Scripts\activate base 
conda activate --stack your\path\to\anaconda3

I'm not sure about is this the easiest way to reactivate anaconda, but this is what worked for me.

0

This is for those who are using ubuntu to run their script.py you have to copy cv2.so under your python pachkages dir and then resourcing your bachrc. here:

sudo cp -rfv /home/<user>/<your opencv file dir>/lib/cv2.so  /usr/local/lib/python2.7/site-packages/
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
0

This Question Have easy Solutions

  1. Install Anconda Navigator
  2. Then Go to Environment And Search "Opencv"
  3. Add the Tick Mark and Apply Install
  4. Finally slove the Problem

Like That

0

The best way for me to solve this problem was to install python3-venv and create a new environment, then install my packages, because any method presented here could not solve the problem and always resulted in the same error. Maybe my solution could be helpful to others:

1- install python3-venv
2- Create and activate new venv
3- pip3 install opencv-python
-1

I worked with the code below and it worked perfectly well;

pip install opencv-python==4.6.0.66enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.