
Photo by Daniil Komov on Pexels
We’ve all been there: it’s 2 AM, you just want to push your code, and suddenly GitHub sends you a polite email telling you that your AWS keys and database passwords are now public property of the entire internet. Let’s fix that once and for all using Python, python-dotenv, and a healthy dose of Git paranoia.
Welcome back to the Old Lane 17 workshop. Today, we are talking about environment variables. Not because they’re sexy, but because forgetting them leads to career-defining panic attacks.
Instead of hardcoding your API keys like an absolute savage, let’s look at how to manage secrets cleanly without setting up a Kubernetes cluster for a simple script.
The Crime Scene: Hardcoding
Picture this: You are building a brilliant automation script. It needs an API key to fetch weather data so you know when to go outside. You write this:
python# weather.py (Please never actually do this)
API_KEY = "super_secret_weather_token_12345"
CITY = "Seattle"
print(f"Checking weather for {CITY} with key {API_KEY}...")
It works! You feel like a wizard. You type git add ., git commit -m "added weather feature", and git push. Five minutes later, an automated bot has already spun up twelve crypto-mining instances using your exposed credentials.
The Fix: Enter .env and Python
The standard, no-BS way to handle this is the .env file paired with python-dotenv. It keeps configuration separate from code, saving you from a midnight conversation with your cloud provider’s billing support.
First, install the library using your terminal:
bashpip install python-dotenv
Next, create a file named .env right in the root of your project directory. This file stays local. It never gets committed.
env# .env
API_KEY=super_secret_weather_token_12345
CITY=Seattle
Now, update your Python script to pull variables from the environment instead of hardcoding them:
python# weather.py
import os
from dotenv import load_dotenv
# Load variables from the .env file into the environment
load_dotenv()
API_KEY = os.getenv("API_KEY")
CITY = os.getenv("CITY")
if not API_KEY:
raise ValueError("Yikes! Missing API_KEY in environment.")
print(f"Checking weather for {CITY} safely!")
The Ultimate Shield: Your .gitignore
This entire system collapses if you commit the .env file itself. To protect yourself from muscle memory, make sure your .gitignore file includes it immediately.
bash# .gitignore
.env
__pycache__/
venv/
.DS_Store
If you’ve already accidentally committed a .env file in the past, a simple git rm --cached .env won’t clean up your history, but it will at least stop you from tracking future changes. (And yes, if it’s already on GitHub, rotate those keys immediately—consider them burned).
By keeping your terminal workflow clean, utilizing simple environment files, and leaning on lightweight CLI practices, you can ship code fast without accidentally funding a hacker’s vacation home. Stay safe out there on Old Lane.
Published by the Old Lane Developer Desk.
Disclaimer: This article was generated and formatted with the assistance of Artificial Intelligence.


