Jupyter notebook not recognizing packages in venv

Question:

This has been driving me crazy. For some reason it seems like my .ipynb file doesn’t recognize the python packages in my venv. I get this error even though I’ve clearly installed opencv. I run into this exact same problem for dlib and imutils too.

enter image description here

However, when I use opevcv in a regular .py file in the save venv, it works completely fine so it’s something to do with .ipynb (Jupiter Notebooks).

I have tried pip3 install opencv-python

Note: I am pretty sure I have selected the correct venv when running my .ipynb file.

The output I get when I run !pip list in the Jupyter Notebook (which includes opencv-python):

Package                 Version
----------------------- -------------------
absl-py                 0.12.0
astunparse              1.6.3
cachetools              4.2.1
certifi                 2020.12.5
chardet                 4.0.0
cmake                   3.18.4.post1
cycler                  0.10.0
dlib                    19.22.0
flatbuffers             1.12
gast                    0.4.0
google-auth             1.29.0
google-auth-oauthlib    0.4.4
google-pasta            0.2.0
grpcio                  1.34.1
h5py                    3.1.0
idna                    2.10
imutils                 0.5.4
keras-nightly           2.5.0.dev2021032900
Keras-Preprocessing     1.1.2
kiwisolver              1.3.1
Markdown                3.3.4
matplotlib              3.4.1
numpy                   1.19.5
oauthlib                3.1.0
opencv-python           4.5.1.48
opt-einsum              3.3.0
Pillow                  8.2.0
pip                     21.0.1
protobuf                3.15.8
pyasn1                  0.4.8
pyasn1-modules          0.2.8
pyparsing               2.4.7
python-dateutil         2.8.1
requests                2.25.1
requests-oauthlib       1.3.0
rsa                     4.7.2
setuptools              54.1.2
six                     1.15.0
tensorboard             2.5.0
tensorboard-data-server 0.6.0
tensorboard-plugin-wit  1.8.0
tensorflow              2.5.0rc1
tensorflow-estimator    2.5.0rc0
termcolor               1.1.0
typing-extensions       3.7.4.3
urllib3                 1.26.4
Werkzeug                1.0.1
wheel                   0.36.2
wrapt                   1.12.1

Please help, thank you

Asked By: Ken

||

Answers:

Try changing kernels. (Check on the image in the URL for guidance). If your venv is not in the list, you should add it manually. Follow this guide to add your venv

Answered By: Gusti Adli

Are you using your local machine? How do you fire up the jupyter notebook? You could try the following:

  1. Open a prompt and activate your virtual environment.
  2. python And then >>> import <module>: Does it import it?

Make sure that you have jupyter insatlled in that environment and then

  1. Open a prompt and activate your virtual environment.
  2. Issue python -m jupyter notebook
Answered By: errorLogger

Maybe this question will be deleted but this is what fixed it for me (thanks to @Shi XiuFeng):

I had initially created my venv with virtualenv env which didn’t work, and so when I recreated the venv with python3 -m venv env this worked completely fine.

Answered By: Ken

It is possible that jupyter notebook is not using "virtualenv". Try following commands to run ipython/jupyter on current/active virtual environment as given here

  1. source env/bin/activate
  2. pip install ipykernel
  3. ipython kernel install --user --name example_venv
  4. ipython or jupyter notebook
Answered By: Yashashvi