cv2 import error on Jupyter notebook

116,183

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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/

Solution 5

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.

Share:
116,183
Hiroyuki Nuri
Author by

Hiroyuki Nuri

Updated on January 08, 2022

Comments

  • Hiroyuki Nuri
    Hiroyuki Nuri over 2 years

    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 ?

  • Hiroyuki Nuri
    Hiroyuki Nuri almost 8 years
    Thank you, I installed cv2 in the Anaconda2 file and it works.
  • ElMix
    ElMix about 7 years
    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
  • Ronald
    Ronald almost 4 years
    Very short answers, trivial answers or in this case a suggestion that might work, can best be placed in a comment.
  • Wayne
    Wayne about 2 years
    Please use & suggest to others the more modern magic command %pip install <package> 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 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
    Wayne about 2 years
    For Python 3-backed Jupyter, please now use the more modern %pip install <package> magic command from inside a notebook. See my comment on this same page here for more about it.
  • OneCricketeer
    OneCricketeer about 2 years
    You shouldn't modify OS paths with virtualenvs