← Lesson
Setup: Python, Keys, and Safety
Joeven
Run
Reset
Python loads on first run
import os # DEMO: we simulate env vars. In real code, set them in the OS. os.environ["WEATHER_API_KEY"] = "sk-demo-not-real" def load_key(name): value = os.environ.get(name, "").strip() if not value: raise RuntimeError("Missing environment variable " + name) if value.startswith("sk-demo"): return value # allowed in this classroom return value key = load_key("WEATHER_API_KEY") print("loaded key length:", len(key)) print("redacted prefix:", key[:7] + "...") print("never print the full key in production logs") missing_name = "OTHER_API_KEY" missing = os.environ.get(missing_name, "").strip() print("missing key empty:", missing == "")
Run to execute this in your browser. Nothing is sent to a server.