Environments & Configuration
Your app runs in different places โ your laptop, a test server, production โ and each needs different settings. Handling that cleanly, and keeping secrets safe, is the foundation of deployment.
The same app runs in different environments: your laptop while developing, maybe a staging server for testing, and production where real users are. Each needs different settings - different database, different keys, different behaviour. Handling this cleanly, through configuration rather than hard-coded values, is a foundation of deployment - and it's where the secrets discipline you learned really matters. This session makes your app deployment-ready.
Dev, staging, production
Professionals run an app in distinct environments so they can build and test safely without touching what real users see.
| Environment | Purpose |
|---|---|
| Development | Your machine, where you build |
| Staging (optional) | A production-like place to test |
| Production | The real thing, real users, real data |
The golden rule: never test risky changes in production. You develop locally, ideally test in a staging environment that mirrors production, and only deploy to production when you're confident. Each environment has its own configuration - the production database is not the one you experiment with locally.
Configuration, not hard-coding
Because settings differ per environment, you don't hard-code them - you read them from configuration, typically environment variables or a config file, so the same code runs anywhere.
import os
from larzenv import load, get
# read config from the environment (or a .env file in development)
env = load(".env") if os.path.exists(".env") else os.environ
DATABASE_URL = get(env, "DATABASE_URL")
DEBUG = get(env, "DEBUG", default=False, cast=bool).env file), not have them written into the source. That way the identical code runs on your laptop and in production, each supplied with its own settings - and, crucially, secrets live in the environment, never in your code or Git history. This is exactly what larzenv and the GIT-101 secrets lesson prepared you for. It's the single most important habit for deploying safely.Deployment-ready by config
- Environments - develop locally, test in staging, run in production; never test risky changes in prod.
- Configuration, not hard-coding - read settings from env vars / a config file.
- Same code everywhere, each environment supplying its own config.
- Secrets in the environment, never in code (larzenv + gitignore).
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- The same app runs in different environments (development, staging, production), each needing different settings.
- Never test risky changes in production - develop locally, test in staging, deploy to prod when confident.
- Read settings from configuration (environment variables / a config file), never hard-code them, so one codebase runs anywhere.
- Secrets live in the environment, never in code or Git - exactly what larzenv and the GIT-101 secrets lesson set up.
Frequently asked
Why not just change the settings in my code when I deploy to production?
Because editing your code for each environment is error-prone, unsafe, and defeats the whole point of having one reliable codebase - and it's exactly the problem that configuration solves. If you hard-code settings and change them by hand when deploying, several bad things follow. You risk mistakes: forgetting to change a value, changing the wrong one, or accidentally committing production settings (or worse, production secrets) into your code and Git history, where they're exposed. You create divergence: the code you tested locally isn't quite the code running in production (because you edited it), so 'it worked in testing' means less. You make deployment manual and fragile: every deploy involves careful hand-edits rather than just running the same code with different inputs. And you can't easily have the same code in multiple places at once (local, staging, production) because they'd each need different hard-coded values. The clean approach is to write your code once to read its settings from configuration - environment variables or a config file that lives outside the code - so the identical, tested codebase runs everywhere, and each environment simply supplies its own configuration values (its own database URL, its own keys, its own debug setting). Your laptop provides development settings (often via a .env file), production provides production settings (usually via environment variables set on the server or platform), and the code doesn't change at all between them. This is safer (secrets stay out of code, no risky hand-edits), more reliable (you deploy exactly what you tested), and more flexible (the same code runs in any environment). It's considered a foundational best practice for deployable apps precisely because it eliminates a whole category of errors and security risks. So rather than changing code per environment, externalise anything that varies into configuration, and let the environment feed the code its settings - one codebase, many environments, secrets never hard-coded. Tools like larzenv make reading that configuration easy, and it's the habit that makes deployment smooth and safe.
What's the difference between development and production, and do I need a staging environment too?
Development and production are the two essential environments, and staging is a valuable-but-optional middle ground you add as projects get more serious. Development is your local setup - your own machine, where you write and run the code as you build it. It's configured for convenience and safety: it typically uses a separate test database (so you can't harm real data), often has debugging features turned on (detailed error messages to help you fix things), and uses fake or sample data. Nothing you do here affects real users, so you can experiment freely. Production is the real deployment - the always-on server where actual users interact with your app using real data. It's configured for reliability and security: debugging details are turned off (you don't want to leak internal information to users or attackers), it uses the real database with real user data, and it needs to stay up and perform well. The critical rule is to never test risky or unfinished changes directly in production, because bugs there affect real people and real data - you develop and test elsewhere, then deploy to production only when confident. Staging is an optional third environment that sits between the two: a production-like setup (same kind of server, similar configuration, perhaps a copy of production data or realistic test data) where you can test your changes in conditions that mirror production before actually going live. Its value is catching problems that only show up in a production-like environment - things that worked on your laptop but break with real-world configuration, data, or scale - without risking the real production system. Do you need staging? For learning and small personal projects, no - development plus production is perfectly fine, and adding staging would be over-engineering. Staging becomes worthwhile as your app grows more important, has real users who'd be affected by bugs, or has enough complexity that 'worked locally' isn't a strong enough guarantee - then a staging environment to catch issues before they hit users is worth the extra setup. So start with just development (local) and production (deployed), keep them properly separated with their own configuration and databases, follow the never-test-in-production rule, and introduce staging later if and when the stakes justify it. The two-environment model is the foundation; staging is a refinement you add as needed.
Related lessons
From 'Works on My Machine' to Online
Your app runs on your laptop โ but nobody else can reach it. Deployment is the leap to runโฆ
Deploying & Running Your AppGetting Your App Online
The practical steps: choose where to host, get your code there, point a domain at it, and โฆ
Version Control with GitGood Git Habits
Git rewards a few simple habits โ commit often with clear messages, ignore what shouldn't โฆ