Assuming config.py does not exist in the project folder.
medium
A. It will raise a FileNotFoundError
B. It will silently ignore missing file
C. It will set SECRET_KEY before loading file
D. It will overwrite SECRET_KEY with None
Solution
Step 1: Understand from_pyfile behavior
from_pyfile tries to load a Python file and raises an error if missing.
Step 2: Check file existence
Since config.py does not exist, a FileNotFoundError occurs immediately.
Final Answer:
It will raise a FileNotFoundError -> Option A
Quick Check:
Missing config file causes error [OK]
Hint: Missing config files cause errors, not silent skips [OK]
Common Mistakes:
Thinking Flask ignores missing config files
Assuming config keys set before loading file are overwritten
Expecting None values on missing keys
5. You want to load configuration in Flask from environment variables but provide default values if they are missing. Which code snippet correctly achieves this?
hard
A. app.config['DEBUG'] = os.environ['DEBUG'] or False
B. app.config['DEBUG'] = os.getenv('DEBUG') or None
C. app.config['DEBUG'] = os.getenv('DEBUG', False)
D. app.config['DEBUG'] = os.environ.get('DEBUG')
Solution
Step 1: Understand environment variable access
os.getenv('KEY', default) returns the environment value or default if missing.
Step 2: Analyze each option
app.config['DEBUG'] = os.getenv('DEBUG', False) uses os.getenv with default False, correctly providing fallback. app.config['DEBUG'] = os.environ['DEBUG'] or False may raise KeyError if 'DEBUG' missing. app.config['DEBUG'] = os.environ.get('DEBUG') returns None if missing but no default. app.config['DEBUG'] = os.getenv('DEBUG') or None returns None if missing, not False.
Final Answer:
app.config['DEBUG'] = os.getenv('DEBUG', False) -> Option C
Quick Check:
Use getenv with default for safe config [OK]
Hint: Use os.getenv with default to avoid missing keys [OK]
Common Mistakes:
Using os.environ[] without checking existence
Not providing default values
Confusing os.environ.get with os.getenv
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.