
Photo by Jakub Zerdzicki on Pexels
We’ve all been there: it’s 2:00 AM, you’re hyped on cold brew, and you hardcode your database password directly into a Python script just to see if the query works. Fast forward three hours, and you’re accidentally pushing a live API key to a public GitHub repository, frantically trying to rewrite Git history while questioning your career choices. The fix isn’t some multi-million-dollar enterprise vault—it’s a clean, disciplined .env workflow paired with Python and python-dotenv.
Here at Old Lane 17, we like our coffee dark, our code clean, and our secrets strictly out of version control. Let’s look at how to set up environment variables properly so you can sleep peacefully at night.
Step 1: The Anatomy of a Good .env File
First things first: create a file named .env right in the root of your project directory. This is where your configuration lives. Think of it as the VIP lounge for your sensitive data—nothing gets in unless it’s invited.
env# .env
DATABASE_URL=postgresql://admin:supersecret@localhost:5432/oldlane_db
API_KEY=sk_live_51NotARealKeyTrustMeBro
DEBUG_MODE=True
Now, the golden rule of terminal life: Add .env to your .gitignore immediately. If you skip this step, you might as well print your passwords on a billboard in Times Square.
bash# Terminal execution
echo ".env" >> .gitignore
git status
Step 2: Accessing Variables in Python
Instead of digging through your codebase trying to remember if you called your database URL DB_PASS, DATABASE_PASSWORD, or db_password_pls_work, use the python-dotenv package.
First, install the package via your terminal:
bashpip install python-dotenv
Next, load the environment variables at the absolute entry point of your application (usually main.py or app.py). Here is a clean, straightforward implementation:
python# main.py
import os
from dotenv import load_dotenv
# Load variables from the .env file into the environment
load_dotenv()
# Access them securely via os.environ
DATABASE_URL = os.getenv("DATABASE_URL")
DEBUG_MODE = os.getenv("DEBUG_MODE") == "True"
if not DATABASE_URL:
raise ValueError("Yikes! DATABASE_URL is missing. Check your .env file.")
def main():
print(f"Connecting to database... Debug mode is: {DEBUG_MODE}")
if __name__ == "__main__":
main()
Step 3: The Gotcha – Sharing with the Team
So, how do you share required environment variables with your team without leaking your actual production secrets? You use an .env.example file.
Create a template file containing all the necessary variable names, but leave the values blank or filled with dummy data. Commit this file to Git so your teammates know what ingredients are needed to bake the cake.
env# .env.example
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
API_KEY=your_api_key_here
DEBUG_MODE=True
When a new developer clones the repo, their onboarding workflow becomes blissfully simple:
bashgit clone https://github.com/old-lane-17/my-awesome-project.git
cd my-awesome-project
cp .env.example .env
# Open .env and fill in local dev values
python main.py
No bloat, no complex secret management dashboards, just a straightforward terminal workflow that keeps your credentials safe and your codebase pristine. Now go close that PR and get some sleep.
Published by the Old Lane Developer Desk.
Disclaimer: This article was generated and formatted with the assistance of Artificial Intelligence.


