0Pricing
Flask Academy · Lesson

Use dotenv in Development

Load a .env file for local config.

Use dotenv in Development is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Exporting by Hand Gets Old

Typing export for ten variables every session is tedious and easy to forget. A .env file fixes that for local work.

What a .env File Holds

A .env file is plain text with KEY=value lines. It lives at your project root and feeds variables in automatically.

SECRET_KEY=dev-secret
DEBUG=1

Install python-dotenv

The python-dotenv package reads that file and loads each line into the environment for you.

pip install python-dotenv

Load It in Code

Call load_dotenv early, before you read any config, so os.environ already holds the file values.

from dotenv import load_dotenv
load_dotenv()

Flask Loads It Too

Recent Flask versions auto-load .env and .flaskenv when python-dotenv is installed, so flask run just works.

.flaskenv for Flask Settings

Put Flask-specific knobs like FLASK_APP in .flaskenv, and keep real secrets in .env instead.

FLASK_APP=app.py
FLASK_DEBUG=1

Gitignore the .env File

Add .env to .gitignore so your local secrets never reach the repository by accident.

.env

Share a .env.example

Commit a .env.example listing the needed keys with blank or dummy values, so teammates know what to set.

SECRET_KEY=
DATABASE_URL=

Real Env Vars Win

By default load_dotenv will not overwrite a variable already set in the real environment, which keeps prod safe.

Dev Only, Not Production

Treat .env as a development convenience. In production, let the platform inject real environment variables instead.

Quotes and Comments

A .env file supports # comments and quotes around values with spaces, just like a tiny config format.

GREETING="hello there"  # a quoted value

Quick Check

Why must the .env file be added to .gitignore?

Recap: dotenv

You stored local config in a .env file, loaded it with python-dotenv, gitignored it, and shared a safe example. 🧪

Frequently asked questions

Is the “Use dotenv in Development” lesson free?

Yes — the full text of “Use dotenv in Development” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Use dotenv in Development”?

Load a .env file for local config. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Use dotenv in Development” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Config Classes per Environment
  2. Load Secrets from Environment Variables
  3. Use dotenv in Development
  4. Toggle Debug and Feature Flags
← Back to Flask Academy