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.