3

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

Ken
  • 1,155
  • 2
  • 19
  • 36
  • did you add the venv into the notebook kernel? – Shi XiuFeng Apr 22 '21 at 05:36
  • 1
    check it out. https://janakiev.com/blog/jupyter-virtual-envs/ – Shi XiuFeng Apr 22 '21 at 05:38
  • Yes I ran this command with my venv `ipython kernel install --user --name=.venv` – Ken Apr 22 '21 at 05:39
  • the pip list cmd may run inside the global python env, because it is not the python command, it is just a shell cmd, did you select the right kernel in the notebook kernel dropdown list? – Shi XiuFeng Apr 22 '21 at 05:50
  • ok, if you activate venv in the terminal and open a python repl, can you import the cv package? maybe the installation is in the incorrect env – Shi XiuFeng Apr 22 '21 at 06:00
  • Yes, it works fine when I activate the venv and run import cv2 in a python repl – Ken Apr 22 '21 at 06:01
  • what about `!which python` result in the notebook? – Shi XiuFeng Apr 22 '21 at 06:04
  • When I run it in my Jupyter Notebook it gives me the correct path `Documents/Projects/Personal/Clear Path/backend/env/bin/python` my venv name is env – Ken Apr 22 '21 at 06:06
  • I'm so sorry, it worked after I followed your first step. I first created the venv with virtualenv, but I recreated the venv with python3 -m venv env and this worked! – Ken Apr 22 '21 at 06:17

5 Answers5

3

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
  • Yes the first part works, when I go into my venv, and run python in the shell I can import cv2 no problem. For the second part, when I run `python -m jupyter notebook` I get `venv/bin/python: No module named jupyter`, am I missing something? Do I need to install Jupiter? – Ken Apr 22 '21 at 05:54
  • However, I can run `jupyter notebook` just itself to start the server without any errors – Ken Apr 22 '21 at 05:58
  • 1
    Yes, you have to install jupyter inside the venv, and launch the jupyter notebook from whithin that venv. Activate your venv, and `python -m pip install jupyter`, then `python -m jupyter notebook`. The `python -m` part runs the provided module as a script. In my experience using conda, it eliminates many issues with modules. Without it, for example, you might `jupyter notebook` and end up with a jupyter kernel from your (base) environment. – errorLogger Apr 22 '21 at 18:27
2

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.

Ken
  • 1,155
  • 2
  • 19
  • 36
0

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

Gusti Adli
  • 1,225
  • 4
  • 13
  • Thanks, I have actually already created a new venv and running the kernel in that venv, should I still switch to another kernel? – Ken Apr 22 '21 at 05:49
0

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

If you run into errors try resetting cache of command line using hash -r

Yashashvi
  • 340
  • 3
  • 12
0

I have Jupyter installed globally which led to the above issues when using it inside a virtual environment.

The following steps resolved the issue:

source ./bin/activate (run from the venv directory)

python3 -m pip install jupyter

python3 -m notebook

start a new Notebook using the "Python3 (ipykernel)" from the dropdown.

shubh
  • 21
  • 2