Flask automatically loads environment variables from two files, .env and .flaskenv, as soon as you run the flask command — you just need to install the python-dotenv package. The convention: .flaskenv holds public variables like FLASK_APP so it can be committed, while .env holds secrets like SECRET_KEY and must never be committed. Two traps to remember: files are only loaded when you run through the flask command or app.run(), and app.run() loads the file after module-level code has already run — so reading an environment variable without calling load_dotenv() first still gives you an empty value.

The Problem

The common beginner approach is to hardcode the secret key straight into the code, or export environment variables every time you open a new terminal. Both burn easily: a secret that slips into Git is leaked, and forgetting to export one variable crashes the app at startup with a confusing KeyError. It gets worse when dev, staging, and prod each need a different set of values while the code stays the same.

# app.py — hardcoded and reading "bare" environment variables
app = Flask(__name__)
app.config["SECRET_KEY"] = "my-secret-key" # leaked once it lands in Git
app.config["DATABASE_URL"] = os.environ["DATABASE_URL"] # forget to export and you get KeyError

The Solution

Install python-dotenv, then split your variables into two files: .flaskenv keeps the public ones, .env keeps the secrets. Then call load_dotenv() on the very first line of app.py, before any os.environ read — this works whether you use flask run, run python app.py directly, or deploy with Gunicorn.

pip install python-dotenv
# .flaskenv — public variables, safe to commit
FLASK_APP=app.py
FLASK_RUN_PORT=5001
# .env — secrets, NEVER commit
SECRET_KEY=dev-secret-123
DATABASE_URL=sqlite:///dev.db
# app.py — load_dotenv() must come BEFORE reading environment variables
from dotenv import load_dotenv
load_dotenv()
import os
from flask import Flask
app = Flask(__name__)
app.config["SECRET_KEY"] = os.environ["SECRET_KEY"]
app.config["DATABASE_URL"] = os.environ.get("DATABASE_URL")

Why must load_dotenv() come first, when Flask already loads the file itself? Because the flask command loads the file before importing the app, but app.run() only loads it when the server starts — after module-level code has already read os.environ. I verified this on Flask 3.1.3: running python app.py directly, a value read at module level was still empty even though the variable was present inside requests afterwards. Calling load_dotenv() at the top of the file is the only approach that works in all three cases: flask run, python app.py, and Gunicorn.

Priority order: a variable already set in the shell beats one in .env, and .env beats .flaskenv. To turn off the flask command’s auto-loading entirely, set FLASK_SKIP_DOTENV=1.

Finally, add .env to .gitignore and commit a .env.example file that lists only the variable names, no real values, so anyone cloning the repo knows what to fill in.

Apply It to Your Project

Apply this to the Flask project you are working on right now: create .flaskenv and .env, move SECRET_KEY, the database connection string, and API keys into .env, and remove them from the code — and from Git history if you ever committed them by mistake. From now on each environment needs only its own .env file, the code stays the same, and deploying to a new server is just copying a file.

Related Posts

FAQ

Without python-dotenv installed, does Flask load the .env file?

No. When it sees a .env file but the package is missing, the flask command only shows a hint to install it, then keeps running without loading anything.

When I run the app with Gunicorn, is the .env file loaded?

No. Gunicorn doesn’t go through the flask command and doesn’t call app.run(), so the file isn’t auto-loaded. Calling load_dotenv() at the top of app.py as shown in the Solution section settles it for good.

I put SECRET_KEY in .env, but app.config is still empty — why?

That’s the load-order trap: app.run() loads the file after module-level code has already read os.environ. Put load_dotenv() on the very first line of the file, before any environment variable read, and it’s fixed.

If a variable exists both in the shell and in .env, which one wins?

The shell wins. .env only fills in values for variables that aren’t already set in the environment, so a value you export by hand or that your CI sets is always preferred.

💬 Have you tried this in your own project? If you hit an error or have a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.

Sources