cv2 import error on Jupyter notebook

前端 未结 9 853
误落风尘
误落风尘 2020-12-03 08:48

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

ImportError: No module named cv2

I am

相关标签:
9条回答
  • 2020-12-03 09:24

    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/

    0 讨论(0)
  • 2020-12-03 09:27

    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:

    `python` 
    

    then write:

    import os
    os.path
    

    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.

    0 讨论(0)
  • 2020-12-03 09:31

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

    0 讨论(0)
  • 2020-12-03 09:33

    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

    0 讨论(0)
  • 2020-12-03 09:36

    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.

    0 讨论(0)
  • 2020-12-03 09:38

    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.

    0 讨论(0)
提交回复
热议问题