You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Google Colab中创建可保存至Google Drive且持久化依赖库的虚拟环境

Persistent Virtual Environment in Google Colab (Stored in Google Drive)

Got it, let's solve this once and for all—no more wasting time reinstalling OpenCV, Pandas, Numpy, or any other dependencies every time you fire up a Colab session. Here's a straightforward, repeatable workflow to create a virtual environment that lives in your Google Drive, so your dependencies stay intact between sessions:

Step 1: Mount Your Google Drive

First, we need to link Colab to your Drive so we can save the environment there permanently. Run this code block and follow the authorization prompt to connect your account:

from google.colab import drive
drive.mount('/content/drive')

This will map your Drive to the /content/drive path in Colab—all our environment files will live here from now on.

Step 2: Create the Virtual Environment

Colab comes with venv pre-installed, so we'll use that for simplicity. Run this command to create your environment in Drive (feel free to replace colab_env with a name that makes sense for your project):

!python -m venv /content/drive/MyDrive/colab_env

This creates a folder colab_env in your Drive's root directory—this is where all your environment's binaries and installed packages will be stored.

Step 3: Activate the Environment & Install Dependencies

Now we need to activate the environment and install your required packages. In Colab, we'll chain the activation and pip install commands to ensure we're installing directly into our Drive-based environment (not Colab's default temporary one):

!source /content/drive/MyDrive/colab_env/bin/activate && pip install opencv-python pandas numpy

Add any other dependencies you need to the end of the pip install line—they'll all be saved in your Drive environment.

Optional: Save Your Dependencies List

To keep track of exactly what's installed (and make it easy to restore if needed), save a requirements.txt file to your environment folder:

!source /content/drive/MyDrive/colab_env/bin/activate && pip freeze > /content/drive/MyDrive/colab_env/requirements.txt

Step 4: Reuse the Environment in Future Sessions

Next time you open Colab, you don't need to recreate the environment—just follow these two quick steps:

  1. Mount your Drive again (same as Step 1)
  2. Activate the environment and start working:
!source /content/drive/MyDrive/colab_env/bin/activate

If you ever need to reinstall dependencies (e.g., if you accidentally modify the environment), you can use the saved requirements.txt:

!source /content/drive/MyDrive/colab_env/bin/activate && pip install -r /content/drive/MyDrive/colab_env/requirements.txt

Quick Tips to Avoid Headaches

  • Always activate before installing: If you skip activation, you'll install packages to Colab's default environment, which won't persist after the session ends.
  • Verify your pip path: After activation, run !which pip to confirm it points to /content/drive/MyDrive/colab_env/bin/pip—this ensures you're using the environment's pip, not the global one.
  • Watch Drive storage: Virtual environments can take up space, especially with large packages like OpenCV. Keep an eye on your Drive's storage limits if you're working with multiple environments.

内容的提问来源于stack exchange,提问作者Sahar Ben Mabrouk

火山引擎 最新活动