18

I'm trying to import cv2 on Jupyter notebook but I get this error:

ImportError: No module named cv2

I am frustrated because I'm working on this simple issue for hours now. it works on Pycharm but not on Jupiter notebook. I've already installed cv2 into Python2.7's site packages, configured Jupyter's kernel to python2, browsed the documentation but I still don't get what I am missing ?

(I'm using windows 10 and working with microsoft cognitives api, that's why I need to import this package.)

here is the code:

 <ipython-input-1-9dee6ed62d2d> in <module>()
----> 1 import cv2
      2 cv2.__version__

What should I do in order to make this work ?

Horai Nuri
  • 5,358
  • 16
  • 75
  • 127

15 Answers15

19

Is your python path looking in the right place? Check where python is looking for the module. Within the notebook try:

import os
os.sys.path

Is the cv2 module located in any of those directories? If not your path is looking in the wrong place. If it is overlooking the install location, append it to your python path. You can follow the instructions here.

Community
  • 1
  • 1
plfrick
  • 1,109
  • 12
  • 12
  • Thank you, I installed cv2 in the Anaconda2 file and it works. – Horai Nuri Jun 29 '16 at 21:50
  • I had the same issue. Had to install Jupyter on the active environment in order to link the jupyter notebook to the envinronment that had opencv2 package installed – ElMix Apr 27 '17 at 21:40
13

I didn't have the openCV installation in my Python3 kernel, so I installed it by activating the specific environment and running this in the command prompt:

pip install opencv-python

How to find and activate my environment?

To list all of Your conda environments, run this command:

conda info --envs

You will get something like this:

ipykernel_py2            D:\Anaconda\envs\ipykernel_py2
root                     D:\Anaconda

After that, activate the environment that is complaining for the missing cv2 and run the pip install opencv-python command.

How to activate an environment?

Just run the command:

activate env_name

where env_name is the wanted environment (for example, You could type activate ipykernel_py2 if You wanted to access the first of the two environments listed above).

Note: If You are on Linux, You need to type source activate env_name.

Aleksandar
  • 3,558
  • 1
  • 39
  • 42
8

Go to your notebook, in menu section

kernel -> Change kernel -> Python<desired version>

Now in the notebook run following command to install opencv2 in the selected environment kernel

python2:

!pip install opencv-python

python3:

!pip3 install opencv-python

Shoaib Mohammed
  • 986
  • 2
  • 9
  • 17
  • 2
    For Python 3-backed Jupyter, please now use the more modern `%pip install ` magic command from inside a notebook. See my comment [on this same page here](https://stackoverflow.com/a/70628570/8508004) for more about it. – Wayne Feb 25 '22 at 22:36
6

To make this clear for those who are having the same issue:

By default: Anaconda (jupyter notebook) has its own version of Python & packages once it has been installed on your PC.

If you have Python x.x installed on your PC, and you installed OpenCV or -whatever packages- using the package manager of this python version, it does NOT mean your jupyter notebook will get access to these python packages you installed earlier. They are not living in the same folder.

To illustrate this, open your windows CMD and write :

python

then write:

import os
os.path

you will get the path of your python. in my case (C:\Python35)

Now open the Anaconda Prompt and write the same commands again:

you will get the anaconda's python path. In my case (C:\Users\MY_NAME\Anaconda3).

As you can see, there are two different paths of python, so make sure that your first step in diagnosing such error (No module named x) is to ask yourself whether you installed the package in the right place or not!

N.B: within Anaconda itself you can create environments, each environment may have different packages installed in it, so you also have to make sure you're in the right environment and it is the active one.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
binmosa
  • 967
  • 1
  • 7
  • 9
  • That is good explanation, however I open my terminal from anaconda (ui) itself (Ancaconda, enviroment, then i select the enviroment, then open terminal, and from there I do pip install, so if this goes into the wrong, that I was assuming should go to the right one, so how can I void this error?, and why doesnt happens to all packages? – Manza Nov 01 '22 at 02:25
6

Binmosa's explanation is great and to the point. As an alternative (easier, but I'm pretty sure it's just a band-aid fix), if you write:

    import sys
    !{sys.executable} -m pip install opencv-python

directly into your notebook, you'll be able to actually install the module in the notebook itself.

The longer explanation is interesting and informative, though. Link: https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/

Ben Witte
  • 61
  • 1
  • 3
3

It is because of opencv library. Try running this command in anaconda prompt:

conda install -c conda-forge opencv
Léopold Houdin
  • 1,515
  • 13
  • 18
3

You can simply open Jupyter Notebook and in any of the cell, just write:

pip install opencv-python

It will automatically install the file Note : Keep turn ON your Internet connection

Then in next cell :

import cv2

It will work.

Rjraj
  • 51
  • 4
1

I added \envs\myenv\Library\bin also in the path variable and it got solved.

1

You will need to install ipykernel for the jupyter notebook. Follow the following steps:

python -m virtualenv env
source env/bin/acitivate
pip install opencv-contrib-python
pip install ipykernel --upgrade
python -m ipykernel install --user
jupyter notebook
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
1
You can simply try this in your jupyter notebook cell  `%pip install opencv-python`

no matter which python version you're using. you may need to restart kernel to use updated package

amaar
  • 97
  • 8
0

I had this issue in my Jupyter Notebook after I had "installed" the opencv package, using Anaconda Navigator, on my base (root) environment.

However, after "installing" the package and its dependencies, Anaconda Navigator showed a reminder popup to update to the next Anaconda Navigator version. I ignored this at first, but couldn't use the opencv package in my Jupyter Notebook.

After I did update Anaconda Navigator to the newer version, the opencv package install worked fine.

Sander Vanden Hautte
  • 2,138
  • 3
  • 22
  • 36
0
pip install opencv-python

This solved the error for me in MacOS.

Ag371
  • 1
0

I had similar problem. None of the above solution worked for me. I did below in my notebook and that solved the issue

!pip install opencv-python
!pip install opencv-python-headless
nad
  • 2,640
  • 11
  • 55
  • 96
  • 1
    Please use & suggest to others the more modern magic command `%pip install ` when running a similar command in your notebook. It automatically installs to the environment backing the notebook in which you are executing it. Please see [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more information. Because automagics are usually on in Jupyter these days you are better off without the `!` and it will add the `%` automatically, that's why @Rjraj's answer works. – Wayne Feb 25 '22 at 22:33
0

I hope you have already activated the environment you know OpenCV is installed in but is not running/import error in jupyter notebook. If not then run the below command and activate your environment before running the jupyter notebook.

conda activate /Users/prajendr/anaconda3/envs/cvpy39

Then, check all the anaconda environments on your machine using the below command on the jupyter notebook.

!conda info --envs

The output would be similar -

enter image description here

Try to install OpenCV in the environment again. enter image description here

You know that you have OpenCV installed in this anaconda environment - cvpy39 and the path is "/Users/prajendr/anaconda3/envs/cvpy39/lib/python3.9/site-packages"

Then type the below commands to see if the OpenCV path was imported in the notebook or not?

import os
os.sys.path

enter image description here

you see the OpenCV path is not in this list so you need to manually import it.

Then in a cell type the below set of code. Make sure to change the python path of the environment to yours.

import sys
path_to_module = "/User/prajendr/anaconda3/envs/cvpy39/lib/python3.9/site-packages/"
sys.path.append(path_to_module)
import cv2

You will now be able to import OpenCV to your jupyter notebook.

Pallawi.ds
  • 103
  • 1
  • 7
-1

One of possibility is that you could have written import cv2 and its utilisation in separate cells of jupyter notebook.If this is the case then first run the cell having import cv2 part and then run the cell utilising the cv2 library.