0
0
Flaskframework~10 mins

Environment variable management in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Environment variable management
Start Flask app
Load .env file (optional)
Read environment variables
Use variables in app config
Run app with config
Access variables anywhere in code
End
This flow shows how Flask loads environment variables, optionally from a .env file, then uses them in app configuration and code.
Execution Sample
Flask
from flask import Flask
import os

app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'default')
This code creates a Flask app and sets its SECRET_KEY config from an environment variable or a default.
Execution Table
StepActionEnvironment Variable AccessedValue RetrievedApp Config ChangeOutput/Result
1Start Flask appNoneN/ANoneApp instance created
2Load .env file if existsSECRET_KEYLoaded if presentNoneEnvironment variables available
3Read SECRET_KEY from environmentSECRET_KEYe.g. 'mysecret123'app.config['SECRET_KEY'] = 'mysecret123'Config set
4If SECRET_KEY missing, use defaultSECRET_KEYdefaultapp.config['SECRET_KEY'] = 'default'Config set with default
5Run app with configSECRET_KEYUsed internallyConfig used in appApp runs securely
6Access SECRET_KEY anywhereSECRET_KEYe.g. 'mysecret123'No changeApp uses key for sessions
7EndNoneN/ANoneApp stops
💡 Execution stops when the app ends or is stopped by user.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
SECRET_KEYNot setLoaded from .env or OS envValue from env or NoneDefault if NoneFinal value in app.config
Key Moments - 3 Insights
Why do we use os.getenv('SECRET_KEY', 'default') instead of just os.environ['SECRET_KEY']?
os.getenv returns None or a default if the variable is missing, preventing errors. os.environ['SECRET_KEY'] raises an error if SECRET_KEY is not set. See execution_table step 4 for default usage.
How does Flask know to load variables from a .env file?
Flask itself doesn't load .env files automatically. You use a package like python-dotenv to load .env variables into the environment before Flask reads them. See execution_table step 2.
Can environment variables change while the app is running?
No, environment variables are read once at startup. Changing them later won't affect the running app unless you reload or restart it. See variable_tracker final state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does app.config['SECRET_KEY'] get if the environment variable is missing?
AAn error is raised
B'default'
CNone
D'mysecret123'
💡 Hint
Check step 4 in the execution_table where default is assigned if SECRET_KEY is missing.
At which step does the app read environment variables into its config?
AStep 5
BStep 1
CStep 3
DStep 7
💡 Hint
Look at the execution_table step where app.config['SECRET_KEY'] is set from environment.
If you add a new environment variable after the app starts, what happens?
AThe app ignores it until restarted
BThe app automatically uses the new variable
CThe app crashes
DThe variable is deleted
💡 Hint
Refer to key_moments about environment variables changing during runtime.
Concept Snapshot
Environment variables store config outside code.
Use os.getenv('VAR', default) to read safely.
Load .env files with python-dotenv before app start.
Set Flask config from env vars for security.
Env vars read once at startup, not live updated.
Full Transcript
This visual execution trace shows how Flask manages environment variables. First, the app starts and optionally loads variables from a .env file using a helper like python-dotenv. Then, it reads variables like SECRET_KEY using os.getenv, which safely returns a default if missing. The app config is set with these values and used during runtime. Environment variables do not change while the app runs, so any updates require a restart. This approach keeps sensitive info like keys out of code and allows easy config changes per environment.