Skip to main content

Handles environment variables in Google Colab notebooks

Project description


Code style: black


This Python package handles environment variables in Google Colab. Environment variables are an important infrastructure component e.g. containing secrets such as API keys that ought not to be included directly in the codebase. At the time of writing, however, Google Colab does not have built-in support for environment variables.

Our solution is to use the python-dotenv package in concert with Colab's built in authorisation tool for Google Drive. The package will attempt to

  1. mount Google Drive locally
  2. load environment variables from a file called vars.env


To load environment variables using colab-env you should include the following code at the top of your Colab notebook:

!pip install colab-env -qU
import colab_env

This will usually open the authentication flow. We use this authentication step to protect any secrets in vars.env.

Remember not to expose these secrets in the outputs of any cells!

When the authentication challenge is passed, the environment variables will either be loaded into the Google Colab environment, or vars.env will be created in your Google Drive.

To modify environment variables using colab-env you should do the following:

!pip install colab-env -qU
from colab_env import envvar_handler

... and then use envvar_handler's add_env and del_env methods to add/modify and delete environment variables respectively from vars.env.

Take it for a test-drive

Simply open up colab-env/colab_env_testbed.ipynb in Google Colab and try it out!


Paddy Alton (

(with thanks to the Apolitical engineering team for assistance and review)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colab-env-0.2.0.tar.gz (4.7 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page