I'm currently having problems getting Jupyter to connect to my kernel that I made using my virtualenv.
Here's what I did:
I first created my virtual environment by doing:
python3 -m venv venv
Then I activated the venv in my terminal, and installed the packages that I needed for my project:
pip install numpy pandas matplotlib ipykernel jupyter jupyterlab
Then I created a kernel from my venv so that Jupyter can use by doing:
python3 -m ipykernel install --user --name=python_data_analytics
Afterwards, I tried to get VSCode to use my venv and I got it to use the interpreter at bin/ipython3 of my venv. As for Jupyter, I got it to use the kernel I made from a dropdown menu.
Then when I tried to execute the first cell of my notebook to import my libraries, Python says that it can't find my libraries.
Here's a screenshot of the end result:
It seems that Jupyter is not using the virtual environment that I made and it's falling back to the system's interpreter. I'm not sure how should I go about fixing this problem. Any help is much appreciated. Thank you.
I'm not sure, but in the left bottom option, where there is a label called "Python 3.9.5..." you can select your desired environment.
I've hit a dead end trying to solve/debug this issue which doesn't seem like it should be that difficult.
I'm working in Pycharm IDE (not the professional) and I'm working inside a virtual environment let's call it pythonProject and I want to be able to run launch a jupyter notebook in this environment so that it can pick up all the python packages i've installed and configured for this environment.
As I understand it from the documentation, these are the steps I need to take.
My terminal prompt statement:
(pythonProject) oliver#oliver-u20:~/pythonProject$
commands:
python3 -m pip install ipykernel
python3 -m pip install notebook
python3 -m ipykernel install --user --name pythonProject --display-name "Python (pythonProject)"
jupyter notebook
But when I load jupyter notebook, it only shows python3 under kernels.
I tried outputting
jupyter kernelspec list
And get only the base kernel which suggests from this that it's not finding my kernelspec, but I can't seem to figure from the documentation what i'm supposed to do.
Am I missing something?
Ok so I've solved this.
I think there was an install issue with jupyter.
I tried reproducing this in a completely new project and venv and could get the kernel showing.
In the project and venv where I still couldn't, I noticed a discrepancy in the output of my jupyter --paths
In the working venv I could see under data
/home/oliver/.local/share/jupyter
Which is where the kernels I installed are located.
However in the project that wasn't working there was instead:
/home/oliver/snap/jupyter/6/.local/share/jupyter
I'm guessing this snap path is from how I originally installed jupyter on my Ubuntu via the app store - seemed sensible at the time.
So I uninstalled jupyter, restarted my venv and the jupyter --paths has magically changed so that
/home/oliver/.local/share/jupyter
is present and when I start a jupyter notebook at the command line with
jupyter notebook
I can see all my kernels showing!
I found this quite difficult to debug with the documentation and command help outputs so hope someone else finds this useful.
I created a virtual environment, installed pandas and some other libraries, changed the ipython kernel and then opened jupyter inside my virtual environment. Pandas and other libraries worked fine.
Then i installed fastai in my virtualenv, but it shows ModuleNotFoundError in Jupyter only. It works fine in terminal, when i run !pip freeze inside Jupyter it lists 'fastai', when i try to install it in jupyter with '!pip install fastai' it shows 'Requirement already satisfied' but importing it still gives me 'ModuleNotFoundError'. Check this image for example
All answers on SO to this question are for people who haven't changed their jupyter kernel to their environment or who have had other issues, but i couldn't find my issue.
You have to add the virtualenv to the kernel. Nice discussion is here (Execute Python script within Jupyter notebook using a specific virtualenv).
Assuming virtualenv is working fine (jupyter-notebook and fastai are working), these are the additional steps, I might have tried. In the second line (below) change the "--name=NameOfVirtualEnv" appropriately with the name of your virtualenv.
pip install --user ipykernel
python -m ipykernel install --user --name=NameOfVirtualEnv
After that once you start the Jupyter notebook, you will see the "New" dropdown to the right side .. there you will have your virtual environment with the fastai.
Please let me know the outcome. Curious if it worked for you.
I have problems importing python packages into Jupyter Notebooks. When I open notebooks from the anaconda prompt, nothing can be imported as shown below.
import problem 1
When I open Jupyter Notebook through the anaconda navigator. Most of the module imports work apart from pandas-datareader.
import problem 2
I do not know why pandas-datareader doesn't work because according to prompt it's installed but according to the list of packages in the navigator it is not (unless it's part of pandas).
prompt
navigator
Please help. Thank you.
Two suggestions:
Explicitly make a kernel from your env. Activate the env (e.g. with source activate env) then try:
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
Ref
Restart the notebook, and look for a new option in the kernels dropdown
Second suggestion:
Import datareader before importing datareader.anything, and repeat that pattern across the notebook for all imports.
Please try in your anaconda prompt
for python 2:
pip install matplotlib
for python 3:
pip3 install matplotlib
and use anaconda prompt to open jupyter notebook and see.
Hope it can help you.
You can also just do
#1 if you have environment activate it
conda activate yourenvname
#2 check if your libraries are there
conda list
#3 make a clean kernel
python -m ipykernel install --user
#4 open notebook or lab
jupyter notebook
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.