Recall & Review
beginner
What is the purpose of environment variables in a Flask application?
Environment variables store configuration settings outside the code. They help keep sensitive data like passwords and API keys safe and allow easy changes without modifying the code.
Click to reveal answer
beginner
How do you access an environment variable in Flask using Python?
You use the <code>os</code> module: <br><pre>import os
secret_key = os.getenv('SECRET_KEY')</pre> This gets the value of <code>SECRET_KEY</code> or returns None if it is not set.Click to reveal answer
beginner
Why should you avoid hardcoding secrets directly in your Flask app code?
Hardcoding secrets risks exposing them if the code is shared or uploaded publicly. Using environment variables keeps secrets outside the code and safer.
Click to reveal answer
intermediate
What is the role of a .env file in managing environment variables?
A
.env file stores environment variables locally in key=value format. Tools like python-dotenv load these variables automatically when the app starts.Click to reveal answer
intermediate
How can you load environment variables from a .env file in a Flask app?
Install <code>python-dotenv</code> and add:<br><pre>from dotenv import load_dotenv
load_dotenv()</pre> This loads variables from the .env file into the environment.Click to reveal answer
Which Python module is commonly used to access environment variables in Flask?
✗ Incorrect
The
os module provides os.getenv() to access environment variables.What is the main benefit of using environment variables for configuration?
✗ Incorrect
Environment variables keep secrets like passwords outside the code, improving security.
What file format is typically used to store environment variables locally for Flask development?
✗ Incorrect
The
.env file stores environment variables in key=value pairs.Which package helps load environment variables from a .env file automatically?
✗ Incorrect
python-dotenv loads variables from .env files into the environment.What happens if you try to get an environment variable that is not set using os.getenv()?
✗ Incorrect
os.getenv() returns None if the variable is not found.Explain how to securely manage sensitive configuration data in a Flask app using environment variables.
Think about keeping passwords and keys out of your code files.
You got /4 concepts.
Describe the steps to set up and use a .env file in a Flask project.
Focus on how to load and read environment variables from a file.
You got /4 concepts.