Skip to content

Instantly share code, notes, and snippets.

@rlogwood
Last active January 29, 2026 03:54
Show Gist options
  • Select an option

  • Save rlogwood/720ba42326d9245390f3b162747f050c to your computer and use it in GitHub Desktop.

Select an option

Save rlogwood/720ba42326d9245390f3b162747f050c to your computer and use it in GitHub Desktop.
Upload any lib/*.py files to Google Colab

colab_bootstrap.py

Minimal bootstrap script for running Jupyter notebooks in both local and Google Colab environments.

Purpose

Solves the chicken-and-egg problem of needing setup code before you can load your setup code. This script:

  • Detects whether you're running locally or in Colab
  • Sets up Python paths appropriately
  • Provides a simple way to upload local lib files to Colab
  • Reloads lib modules during development to pick up code changes

Quick Start

Add this to the top of each notebook:

# === COLAB BOOTSTRAP ===
COLAB_HELPER_GIST = 'https://gist.github.com/rlogwood/720ba42326d9245390f3b162747f050c'
BOOTSTRAP_URL = f'{COLAB_HELPER_GIST}/raw/colab_bootstrap.py'
import urllib.request; exec(urllib.request.urlopen(BOOTSTRAP_URL).read().decode())
# === END BOOTSTRAP ===

# Upload lib files if in Colab (run once per session)
upload_lib()  # Prompts you to select lib/*.py files (Colab only)

# Your imports
import lib.text_util as tu
import lib.wrangler as wr

# Reload modules after code changes
reload_lib_modules()

Functions

is_colab()

Returns True if running in Google Colab, False otherwise.

upload_lib(lib_dir=None)

Uploads local Python files to Colab via file picker dialog.

  • lib_dir: Destination directory (default: /content/lib)
  • No-op when running locally
  • Creates __init__.py automatically if missing
  • Run again anytime your lib files change

setup_paths()

Adds the lib directory to sys.path. Called automatically when the script loads.

  • In Colab: adds /content to path
  • Locally: adds ../../ to path (assumes notebooks are 2 levels below the lib folder)

reload_lib_modules()

Reloads all currently imported lib.* modules.

  • Auto-discovers which lib modules are loaded—no hardcoded list needed
  • Run after uploading updated lib files or editing local lib code
  • Prints each module as it's reloaded

Typical Workflow

Initial Setup (once per session)

  1. Run the bootstrap code
  2. In Colab: upload_lib() and select your lib files
  3. Import the modules you need

During Development

When you change lib code:

  1. Local: Just run reload_lib_modules()
  2. Colab: Run upload_lib() to re-upload, then reload_lib_modules()

Project Structure

Expected directory layout:

assignments/
├── lib/
│   ├── __init__.py
│   ├── text_util.py
│   ├── wrangler.py
│   └── ...
├── assignment1/
│   └── notebook.ipynb
└── assignment2/
    └── notebook.ipynb

Notes

  • Lib files are uploaded to /content/lib in Colab
  • The Colab filesystem resets when the runtime disconnects, so you'll need to re-upload lib files each session
  • For data files that don't change often, consider using Google Drive instead
  • The path setup assumes notebooks are 2 levels below the lib folder; modify setup_paths() if your structure differs
import os
import sys
import importlib
def is_colab():
return 'COLAB_RELEASE_TAG' in os.environ
def upload_lib(lib_dir=None):
"""Upload local lib files to Colab."""
if not is_colab():
print("Not in Colab, using local lib files")
return
from google.colab import files
lib_dir = lib_dir or '/content/lib'
os.makedirs(lib_dir, exist_ok=True)
print("Select your lib/*.py files:")
uploaded = files.upload()
for filename, content in uploaded.items():
filepath = os.path.join(lib_dir, filename)
with open(filepath, 'wb') as f:
f.write(content)
print(f" ✓ {filename}")
# Ensure __init__.py exists
init_path = os.path.join(lib_dir, '__init__.py')
if not os.path.exists(init_path):
open(init_path, 'w').close()
def setup_paths():
"""Add lib to Python path."""
if is_colab():
if '/content' not in sys.path:
sys.path.insert(0, '/content')
else:
# Local: lib is 2 levels up from notebook
assignments_path = os.path.abspath('../../')
if assignments_path not in sys.path:
sys.path.insert(0, assignments_path)
def reload_lib_modules():
"""Reload all lib.* modules that are currently imported."""
lib_modules = [name for name in sys.modules if name.startswith('lib.')]
for module_name in lib_modules:
importlib.reload(sys.modules[module_name])
print(f" ✓ Reloaded {module_name}")
if not lib_modules:
print(" No lib modules loaded yet")
# Auto-run on import
setup_paths()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment