Bird
Raised Fist0
Flaskframework~20 mins

Environment-based configuration in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flask Env Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Flask app configuration?
Given this Flask app code, what will be the value of app.config['DEBUG'] when FLASK_ENV is set to production?
Flask
import os
from flask import Flask

app = Flask(__name__)

if os.getenv('FLASK_ENV') == 'development':
    app.config['DEBUG'] = True
else:
    app.config['DEBUG'] = False

print(app.config['DEBUG'])
ATrue
BRaises KeyError
CNone
DFalse
Attempts:
2 left
💡 Hint
Think about what happens when the environment variable is not 'development'.
📝 Syntax
intermediate
2:00remaining
Which option correctly loads environment variables into Flask config?
Which code snippet correctly loads environment variables from a .env file into a Flask app configuration using python-dotenv?
A
from dotenv import load_dotenv
load_dotenv()
app.config.from_envvar('MY_CONFIG')
B
from dotenv import load_dotenv
load_dotenv()
app.config.from_prefixed_env('MYAPP_')
C
from dotenv import load_dotenv
load_dotenv()
app.config.from_mapping(os.environ)
D
from dotenv import load_dotenv
load_dotenv()
app.config.from_object('config')
Attempts:
2 left
💡 Hint
Look for the method that accepts a dictionary of environment variables.
❓ state_output
advanced
2:00remaining
What is the value of app.config['DATABASE_URI'] after running this code?
Consider this Flask app configuration code snippet. What will be the value of app.config['DATABASE_URI'] if the environment variable DATABASE_URI is not set?
Flask
import os
from flask import Flask

app = Flask(__name__)
app.config['DATABASE_URI'] = os.getenv('DATABASE_URI', 'sqlite:///default.db')

print(app.config['DATABASE_URI'])
A'sqlite:///default.db'
BNone
CRaises KeyError
DEmpty string ''
Attempts:
2 left
💡 Hint
Check the second argument of os.getenv.
🔧 Debug
advanced
2:00remaining
Why does this Flask app fail to load environment config?
This Flask app tries to load config from environment variables but fails. What is the error in this code?
Flask
import os
from flask import Flask

app = Flask(__name__)
app.config.from_envvar('MYAPP_SETTINGS')

# Assume MYAPP_SETTINGS is set to 'config.py' file path
AThe config file path must be absolute, relative paths cause failure
BMYAPP_SETTINGS must be a Python module, not a file path
Cfrom_envvar expects the environment variable to contain a file path to a config file, so this is correct
DThe config file must be imported with importlib before calling from_envvar
Attempts:
2 left
💡 Hint
Check how Flask expects config file paths in from_envvar.
🧠 Conceptual
expert
2:00remaining
Which statement about Flask environment-based configuration is correct?
Select the correct statement about managing Flask app configuration using environment variables.
AFlask requires restarting the server to detect changes in environment variables
BUsing app.config.from_object() allows switching configs by passing different config classes
CEnvironment variables cannot override values set in config.py files
DFlask automatically loads .env files without any code when FLASK_ENV is set
Attempts:
2 left
💡 Hint
Think about how Flask config classes work.

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

Solution

  1. Step 1: Understand environment-based configuration purpose

    It helps keep different settings for development, testing, and production separate and secure.
  2. Step 2: Identify best practice for config management

    Separating configs avoids hardcoding sensitive data and makes the app flexible across environments.
  3. Final Answer:

    To separate settings for development, testing, and production environments -> Option C
  4. Quick Check:

    Environment-based config = Separate settings [OK]
Hint: Think about keeping dev and prod settings separate [OK]
Common Mistakes:
  • Hardcoding all config in one file
  • Ignoring environment differences
  • Storing secrets in code
  • Not using environment variables
2. Which of the following is the correct way to load configuration from a Python class in Flask?
easy
A. app.config.from_object('config.DevelopmentConfig')
B. app.config.from_file('config.py')
C. app.load_config('config.py')
D. app.config.load('DevelopmentConfig')

Solution

  1. Step 1: Recall Flask config loading methods

    Flask uses from_object to load config from a Python class.
  2. Step 2: Match correct syntax

    app.config.from_object('config.DevelopmentConfig') correctly loads the class named DevelopmentConfig from config module.
  3. Final Answer:

    app.config.from_object('config.DevelopmentConfig') -> Option A
  4. Quick Check:

    Use from_object to load class config [OK]
Hint: Use from_object with full class path string [OK]
Common Mistakes:
  • Using non-existent methods like from_file or load_config
  • Passing filename instead of class path
  • Missing quotes around class path
  • Confusing from_object with from_envvar
3. Given this Flask config setup:
class Config:
    DEBUG = False

class DevelopmentConfig(Config):
    DEBUG = True

app = Flask(__name__)
app.config.from_object(DevelopmentConfig)
print(app.config['DEBUG'])

What will be printed?
medium
A. False
B. KeyError
C. None
D. True

Solution

  1. Step 1: Understand class inheritance

    DevelopmentConfig inherits from Config and overrides DEBUG to True.
  2. Step 2: Check which config is loaded

    app.config.from_object loads DevelopmentConfig, so DEBUG is True.
  3. Final Answer:

    True -> Option D
  4. Quick Check:

    Subclass overrides DEBUG = True [OK]
Hint: Subclass config overrides parent values [OK]
Common Mistakes:
  • Assuming base class value is used
  • Confusing attribute names
  • Expecting KeyError for existing keys
  • Ignoring inheritance
4. What is wrong with this Flask config loading code?
app = Flask(__name__)
app.config.from_envvar('MY_CONFIG')

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

  1. Step 1: Understand from_envvar behavior

    from_envvar expects an environment variable with a filepath; if missing, it raises RuntimeError.
  2. Step 2: Check what happens if MY_CONFIG is not set

    Since MY_CONFIG is missing, Flask raises RuntimeError to alert missing config file.
  3. Final Answer:

    It will raise a RuntimeError because MY_CONFIG is missing -> Option B
  4. 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

  1. Step 1: Check how to read environment variable

    Use os.getenv('FLASK_ENV') to get the current environment string.
  2. Step 2: Use conditional logic to load correct config

    If environment is 'production', load ProductionConfig; otherwise load DevelopmentConfig.
  3. Final Answer:

    if os.getenv('FLASK_ENV') == 'production': app.config.from_object('config.ProductionConfig') else: app.config.from_object('config.DevelopmentConfig') -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard