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 ?
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.
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.
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
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/
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.
It is because of opencv library.
Try running this command in anaconda prompt:
conda install -c conda-forge opencv
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.
I added \envs\myenv\Library\bin also in the path variable and it got solved.
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
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
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.
pip install opencv-python
This solved the error for me in MacOS.
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
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 -
Try to install OpenCV in the environment again.
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
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.
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.
Related
I have downloaded anaconda and always used Jupiter Notebook, and Spyder with no problem. Now I need to run some code on my computer's python shell (I don't know if this is the appropriate name, I am quite new to coding) but when trying to import numpy I get:
ModuleNotFoundError: No module named 'numpy'
I have tried everything whit pip but is not resolving the problem, it actually shows a syntax error. Should I try to uninstall and install everything? I don't really know how to move, thanks a lot in advance.
When you start a new terminal instance the base conda environment is not activated, so when you do python, the interpreter that is called is not Anaconda's Python, it is most likely the system-wide Python installation.
So to access Anaconda's Python from the terminal:
conda activate to activate the base conda environment
which python should show the path to the Anaconda interpreter
python
Interactive shell:
import numpy
Anaconda is a virtual environment for python. It does not install packages globaly. I strongly recomend reading about VENV (virtual enviroment).
You can install does packages globally, by pip:
pip install numpy
or for python3:
pip3 install numpy
I'm working on a new project where I need to install all necessary packages such as OpenCV on a custom environment for the sake of organized-work using anaconda.
1- First, I created and activated the new environment called OPENCV_env
2- Then I installed OpenCV on this environment
3- I added my new environment to jupyter notebook using the following command
python -m ipykernel install --user --name=OPENCV_env
4- I opened jupyter notebook and choase the the new kernel
5- when i try to import OpenCV in jupyter, it returns the following error:
6- I checked whether OpenCV is installed on the custom environment, and it seems to be correctly installed:
So my question is:
Why cv2 cant be imported in Jupyter notebook, especially when I choose the kernel environment that I created?
I've solved the problem just by installing opencv from source. It also works by installing opencv through pip pkg-manager. However, i don't know why jupyter can't import the cv2 when installed using conda.
Link to the source build:
https://www.youtube.com/watch?v=tjXkW0-4gME&t=397s
I have installed the azureml package and can see it in . . .anaconda\lib\site-packages:
If I run import azureml.dataprep as dprep in a python script in Spyder (launched from Anaconda Navigator), it works. But, if I open one of my anaconda environments with jupyter notebook and try running the same line of code, I get an error about module not found for azureml:
I thought perhaps the problem was that the package needed to be installed for that specific environment, but azureml is not available as a package for install via the anaconda environments > install packages interface (there is an azure package but not an azureml package).
So, I followed instructions to use conda prompt to install a package into a specific environment. Instructions I followed:
(those are from this link)
And here is the result of following the instructions (it looked like it installed the package into the env):
But, I got the exact same error when trying to import the package in the environment started as a jupyter notebook. Then, I closed anaconda navigator completely just in case, but that also didn't change the result.
Any ideas about what I'm either doing wrong or how I can manually install this package into a specific anaconda environment?
You did the right thing to install the package into the environment. Btw, pip is automatically installed by conda into any environment that has Python, so installing it shouldn't have been necessary.
Are you sure that the environment that you installed into is the one in which your notebook kernel is running? Start the notebook and execute
!conda env list
That will give you a list of environments, and an asterisk * next to the one that is active.
You can also call pip directly from a notebook cell:
!pip install azureml
That will install into the conda environment in which the kernel is running.
I am currently trying to work basic python - jupyter projects.
I am stuck on following error during matplotlib:
screenshot on jupyter-error
ModuleNotFoundError: No module named 'matplotlib'
I tried to update, reinstall matplotlib aswell in conda and in pip but it still not working.
happy over every constructive feedback
In a Notebook's cell type and execute the code:
import sys
!{sys.executable} -m pip install --user matplotlib
and reload the kernel
(src: http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/ )
open terminal and change the directory to Scripts folder where python installed. Then type the following command and hit enter
pip install matplotlib
Hope this will solve the issue.
I was facing the exact issue. It turns out that it was using the system Python version despite me having activated my virtual environment.
This is what eventually worked.
If you are using a virtual environment which has a name say myvenv, first activate it using command:
source activate myvenv
Then install module ipykernel using the command:
pip install ipykernel
Finally run (change myvenv in code below to the name of your environment):
ipykernel install --user --name myvenv --display-name "Python (myvenv)"
Now restart the notebook and it should pick up the Python version on your virtual environment.
While #Frederic's top-voted solution is based on JakeVDP's blog post from 2017, it completely neglects the %pip magic command mentioned in the blog post. Since 2017, that has landed in mainline IPython and the easiest way to access the correct pip instance connected to your current IPython kernel and environment from within a Jupyter notebook is to do
%pip install matplotlib
Take a look at the list of currently available magic commands at IPython's docs.
generally speaking you should try to work within python virtual environments. and once you do that, you then need to tell JupyterLab about it. for example:
# create a virtual environment
# use the exact python you want to work with in this step
python3.9 -m venv myvenv
# 'activate' (or 'enter') it
source myvenv/bin/activate
# install the exact stuff you want to use in that environment
pip install matplotlib
# now tell JupyterLabs about the environment
python -m ipykernel install --user --name="myenv" --display-name="My project (myenv)"
# start it up
jupyter notebook mynotebook
# if you now look under 'Kernel->Change kernel', your 'myenv' should be there
# select it (restart kernel etc if needed) and you should be good
The issue with me was that jupyter was taking python3 for me, you can always check the version of python jupyter is running on by looking on the top right corner (attached screenshot).
When I was doing pip install it was installing the dependencies for python 2.7 which is installed on mac by default.
It got solved by doing:
> pip3 install matplotlib
Having the same issue, installing matplotlib before to create the virtualenv solved it for me. Then I created the virtual environment and installed matplotlib on it before to start jupyter notebook.
in jupter notebook type
print(sys.executable)
this gave me the following
/Users/myusername/opt/anaconda3/bin/python
open terminal, go into the folder
/Users/myusername/opt/anaconda3/bin/
type the following:
python3 -m pip install matplotlib
restart jupyter notebook (mine is vs code mac ox)
If module installed an you are still getting this error, you might need to run specific jupyter:
python -m jupyter notebook
and this is also works
sudo jupyter notebook --allow-root
When I try to import tensorflow in IPython in my Anaconda environment, I get a No module named tensorflow error. However, when I import it after running the python command in the terminal, there are no errors.
I've googled for solutions and so far I have tried the following:
copied the site-packages inside /anaconda2/lib/python2.7/envs/tensorflow/lib/python2.7/site-packages/ to /anaconda2/lib/python2.7/site-packages/
installed ipython in the conda environment with conda install ipython
Anyone know what else I could try?
Refer: Tensorflow and Anaconda on Ubuntu?
I found a link where the tensorflow.whl files were converted to conda packages, so I went ahead and installed it using the command:
conda install -c https://conda.anaconda.org/jjhelmus tensorflow
and it worked, since the $PATH points to anaconda packages, I can import it now!
Source is here