It means setting up your Flask app to use different settings depending on where it runs, like development, testing, or production. This helps keep things organized and safe.
Click to reveal answer
beginner
How do you tell Flask to use a specific configuration file?
You use app.config.from_object() or app.config.from_pyfile() to load settings from a Python file or object that matches your environment.
Click to reveal answer
beginner
Why should you avoid hardcoding sensitive info like passwords in your Flask config?
Because hardcoding secrets can expose them if your code is shared. Instead, use environment variables or separate config files that are not shared publicly.
Click to reveal answer
intermediate
What is the purpose of the FLASK_ENV environment variable?
It tells Flask which environment it is running in, like 'development' or 'production', so Flask can adjust settings like debug mode automatically.
Click to reveal answer
intermediate
How can you switch configurations without changing your code in Flask?
By setting environment variables outside your code, like FLASK_ENV or custom variables, and loading config based on those variables.
Click to reveal answer
Which Flask method loads configuration from a Python file?
Aapp.set_config()
Bapp.load_config()
Capp.config.from_pyfile()
Dapp.config.load()
✗ Incorrect
app.config.from_pyfile() loads configuration from a Python file.
What does setting FLASK_ENV=production do in Flask?
ADisables debug mode and optimizes for production
BStarts the Flask shell
CRuns tests automatically
DEnables debug mode
✗ Incorrect
Setting FLASK_ENV=production disables debug mode and optimizes Flask for production use.
Why use environment variables for configuration?
ATo hardcode secrets in code
BTo keep sensitive info out of code and easily change settings
CTo slow down the app
DTo avoid using config files
✗ Incorrect
Environment variables keep sensitive info out of code and allow easy changes without code edits.
Which is NOT a common Flask environment?
Adevelopment
Btesting
Cproduction
Ddebugging
✗ Incorrect
'debugging' is not a standard Flask environment; common ones are development, testing, and production.
How can you load different configs based on environment in Flask?
AUse if-else in code checking environment variables
BUse the same config for all environments
CManually edit config files every time
DFlask does not support environment-based configs
✗ Incorrect
You can check environment variables in your code and load different configs accordingly.
Explain how environment-based configuration helps in Flask app development and deployment.
Think about why you wouldn't want the same settings everywhere.
You got /3 concepts.
Describe how to set up and switch between multiple configuration files in a Flask app.
Consider how your app knows which config to use.
You got /3 concepts.
Practice
(1/5)
1. What is the main reason to use environment-based configuration in a Flask app?
easy
A. To avoid using environment variables completely
B. To write all configuration directly in the main app file
C. To separate settings for development, testing, and production environments
D. To hardcode sensitive data like passwords in the code
Assuming the environment variable MY_CONFIG is not set.
medium
A. It will silently ignore missing MY_CONFIG and use default config
B. It will raise a RuntimeError because MY_CONFIG is missing
C. It will load config from a file named 'MY_CONFIG'
D. It will load config from environment variables automatically
Solution
Step 1: Understand from_envvar behavior
from_envvar expects an environment variable with a filepath; if missing, it raises RuntimeError.
Step 2: Check what happens if MY_CONFIG is not set
Since MY_CONFIG is missing, Flask raises RuntimeError to alert missing config file.
Final Answer:
It will raise a RuntimeError because MY_CONFIG is missing -> Option B
Quick Check:
Missing env var causes RuntimeError [OK]
Hint: from_envvar requires env var set or it errors [OK]
Common Mistakes:
Assuming it ignores missing env var
Thinking it loads from env vars automatically
Confusing from_envvar with from_object
Expecting silent fallback to defaults
5. You want your Flask app to load different configs based on the environment variable FLASK_ENV. Which code snippet correctly implements this?
hard
A. if os.getenv('FLASK_ENV') == 'production':
app.config.from_object('config.ProductionConfig')
else:
app.config.from_object('config.DevelopmentConfig')
B. app.config.from_object('config.' + FLASK_ENV + 'Config')
C. app.config.from_envvar('FLASK_ENV')
D. app.config.from_object('config.DefaultConfig')
Solution
Step 1: Check how to read environment variable
Use os.getenv('FLASK_ENV') to get the current environment string.
Step 2: Use conditional logic to load correct config
If environment is 'production', load ProductionConfig; otherwise load DevelopmentConfig.
Final Answer:
if os.getenv('FLASK_ENV') == 'production':
app.config.from_object('config.ProductionConfig')
else:
app.config.from_object('config.DevelopmentConfig') -> Option A
Quick Check:
Use os.getenv + if-else to select config [OK]
Hint: Use os.getenv and if-else to pick config class [OK]
Common Mistakes:
Trying to concatenate string without quotes
Using from_envvar with FLASK_ENV directly
Not handling default environment
Loading only one config regardless of env
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.