0
0
Flaskframework~20 mins

Environment variable management in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Flask access environment variables in this code?

Consider this Flask app snippet that reads an environment variable to configure debug mode:

import os
from flask import Flask

app = Flask(__name__)
app.config['DEBUG'] = os.getenv('FLASK_DEBUG', 'False') == 'True'

@app.route('/')
def home():
    return 'Debug is ' + str(app.config['DEBUG'])

What will be the output if the environment variable FLASK_DEBUG is set to True?

Flask
import os
from flask import Flask

app = Flask(__name__)
app.config['DEBUG'] = os.getenv('FLASK_DEBUG', 'False') == 'True'

@app.route('/')
def home():
    return 'Debug is ' + str(app.config['DEBUG'])
ADebug is True
BDebug is False
CDebug is None
DDebug is 'True'
Attempts:
2 left
💡 Hint

Remember that environment variables are strings and the code compares the string value.

📝 Syntax
intermediate
2:00remaining
Which option correctly loads environment variables from a .env file in Flask?

You want to load environment variables from a .env file in your Flask app. Which code snippet correctly does this using python-dotenv?

A
from dotenv import load_dotenv
load_dotenv()

app = Flask(__name__)
B
import dotenv
dotenv.load()

app = Flask(__name__)
C
from flask_dotenv import load_dotenv
load_dotenv()

app = Flask(__name__)
D
import os
os.load_dotenv()

app = Flask(__name__)
Attempts:
2 left
💡 Hint

Check the official python-dotenv package usage.

🔧 Debug
advanced
2:00remaining
Why does this Flask app fail to read updated environment variables after .env change?

Given this Flask app code:

from dotenv import load_dotenv
import os
from flask import Flask

load_dotenv()
app = Flask(__name__)

@app.route('/')
def index():
    return os.getenv('MY_VAR', 'Not Set')

You update the .env file to change MY_VAR but the app still returns the old value without restarting. Why?

Flask
from dotenv import load_dotenv
import os
from flask import Flask

load_dotenv()
app = Flask(__name__)

@app.route('/')
def index():
    return os.getenv('MY_VAR', 'Not Set')
AThe load_dotenv() function does not read .env files automatically.
BFlask blocks environment variable access inside route functions.
Cos.getenv caches values and does not reflect changes until Python interpreter restarts.
DEnvironment variables are loaded once at startup; changes in .env require app restart to reload.
Attempts:
2 left
💡 Hint

Think about when environment variables are loaded and how Flask apps run.

state_output
advanced
2:00remaining
What is the value of app.config['SECRET_KEY'] after this code runs?

Consider this Flask app snippet:

import os
from flask import Flask

os.environ['SECRET_KEY'] = 'supersecret'
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'defaultkey')

What is the value of app.config['SECRET_KEY']?

Flask
import os
from flask import Flask

os.environ['SECRET_KEY'] = 'supersecret'
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'defaultkey')
ANone
Bdefaultkey
Csupersecret
DRaises KeyError
Attempts:
2 left
💡 Hint

Remember how os.environ and os.getenv work.

🧠 Conceptual
expert
3:00remaining
Which approach best secures sensitive environment variables in Flask deployment?

You want to keep sensitive environment variables like SECRET_KEY safe in production. Which approach is best practice?

AStore sensitive variables in a .env file committed to version control for easy access.
BSet environment variables directly on the production server or container without committing to code.
CHardcode sensitive values in the Flask app source code for simplicity.
DUse Flask's config.from_pyfile() with a config file containing secrets committed to repo.
Attempts:
2 left
💡 Hint

Think about security and version control exposure.