0
0
Flaskframework~5 mins

Environment variable management in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Adotenv
Bsys
Cflask.env
Dos
What is the main benefit of using environment variables for configuration?
AThey make the app run faster
BThey keep sensitive data out of the code
CThey automatically update the code
DThey replace the need for a database
What file format is typically used to store environment variables locally for Flask development?
Aconfig.json
Bsettings.yaml
C.env
Dvariables.txt
Which package helps load environment variables from a .env file automatically?
Apython-dotenv
Bflask-config
Cenv-loader
Dflask-env
What happens if you try to get an environment variable that is not set using os.getenv()?
AIt returns None
BIt returns an empty string
CIt raises an error
DIt returns 0
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.