Before anything else: what are environment variables? In simple terms, they’re key-value pairs that you set up in your operating system to store information. You can think of them like little sticky notes for your computer except instead of being physically attached to your screen or desk, they live inside the memory of your machine.
So why should you care about environment variables? Well, let’s say you have an application that needs to connect to a database. Instead of hardcoding your database credentials into your code (which is a terrible idea for security reasons), you can use environment variables to store them instead. This way, if you need to change the database connection details or move your app to another server, all you have to do is update the environment variable values no need to go through and edit every single line of code in your application!
Here’s an example: let’s say we want to set up a PostgreSQL database for our Python app. We can create an .env file (which stands for “environment variables”) with the following content:
# This script sets up environment variables for a PostgreSQL database in a Python app.
# We create an .env file with the following content:
# DB_HOST: the host name for the database
DB_HOST = "localhost"
# DB_PORT: the port number for the database
DB_PORT = 5432
# DB_USER: the username for the database
DB_USER = "myuser"
# DB_PASSWORD: the password for the database
DB_PASSWORD = "mypassword"
# DB_NAME: the name of the database
DB_NAME = "mydatabase"
Then, we can load these environment variables into our Python code using the `os.environ` dictionary:
# Import necessary libraries
import os # Importing the os library to access environment variables
from dotenv import load_dotenv # Importing the load_dotenv function from the dotenv library
from psycopg2 import connect # Importing the connect function from the psycopg2 library
# Load environment variables from .env file (if it exists)
load_dotenv() # Calling the load_dotenv function to load environment variables from the .env file
# Connect to database using environment variable values
conn = connect( # Creating a connection to the database using the connect function
host=os.environ['DB_HOST'], # Accessing the 'DB_HOST' environment variable and passing it as the host parameter
port=int(os.environ['DB_PORT']), # Accessing the 'DB_PORT' environment variable, converting it to an integer, and passing it as the port parameter
user=os.environ['DB_USER'], # Accessing the 'DB_USER' environment variable and passing it as the user parameter
password=os.environ['DB_PASSWORD'], # Accessing the 'DB_PASSWORD' environment variable and passing it as the password parameter
dbname=os.environ['DB_NAME'] # Accessing the 'DB_NAME' environment variable and passing it as the dbname parameter
)
And that’s it! Now you can easily manage your application configuration and secrets using environment variables, without having to hardcode sensitive information into your code or worry about accidentally exposing it in version control.
But wait there’s more! If you want to take things up a notch (and avoid the potential security risks of storing sensitive data in plain text), you can use a tool like Doppler to manage your environment variables instead. With Doppler, you can store and access your config values using an easy-to-use CLI that works for every language, framework, and platform no more messing around with .env files or trying to remember which environment variable is used where!
It might not be the most glamorous topic in the world of programming, but trust us once you start using them, you’ll wonder how you ever lived without them!