Whenever you close your colab notebook or restart kernel it is highly likely you will lose any third party package you may have installed in your notebook.
It is ok if these packages are small in size, you can install them again whenever you want but as we know some times deep learning packages could be quite large in size even in GBs
So there must be some way or hack to persist these packages so that we need not to install them all the time. This tutorial is all about that only
Step 1:
from google.colab import drive
drive.mount("/content/drive")
let's first mount google drive inside our notebook, when you run this cell a popup window may come in your browser asking for giving colab access to your drive, accept that
Step 2:
virtualenv package is not available in colab by default , you need to install it
Step 3:
Create virtual environment
!virtualenv /content/drive/MyDrive/colab_env_vision
Step 4:
Activate Virtual environment and install required packages (for example i am using loguru
package)
!source /content/drive/MyDrive/colab_env_vision/bin/activate; pip install loguru
Note: Make sure you run pip install in same cell where you are activating your environment
Step 5:
Add environment to system path
import sys
sys.path.append("/content/drive/MyDrive/colab_env_vision/lib/python3.10/site-packages")
Step 6:
from loguru import logger
logger.info("test log")
import package and see it is working fine
Step 7 (Optional):
Close notebook and open again in a new tab to verify if installed package is still accessible
from google.colab import drive
drive.mount("/content/drive")
import sys
sys.path.append("/content/drive/MyDrive/colab_env_vision/lib/python3.10/site-packages")
from loguru import logger
logger.info("test log")