'setting up vitrual environment for deep learning on ubuntu

I am trying to setup virtual environment for every deep learning project that i do locally on my laptop so that every package like numpy, matplotlib, opencv-python etc. i installed will be installed in that environment only not on my laptop globally. Now, the problem is i activate the virtual environment using source ENV_NAME/bin/activate and when i import libraries like cv2 in my notebook after installing cv2 in that environment setup, it says " ModuleNotFoundError: No module named 'cv2'" please see the screenshots below. but when i deactivate that environment and install cv2 on my laptop globally and then try to import cv2, it works fine. Could someone please tell my why?

enter image description here

enter image description here



Solution 1:[1]

You need to restart the kernel in Jupyter Notebook for the new modules to show up.

enter image description here

Solution 2:[2]

Not sure if you already found the solution, but I stumbled upon this blog and it answers most questions of the type: I installed package X and now I can't import it in the notebook. Help!

You can install the package directly in the jupyter notebook using:

# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install opencv-python

if using pip.

OR

# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} opencv-python

if using conda.

Head over to link for more details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rajput
Solution 2 Dharman