Using Environment Variables in Python for App Configuration and Secrets

You might have heard of them before or even used them in your shell scripts, but did you know they can also be incredibly useful for configuring Python applications?

To start, let’s answer the question on everyone’s mind: what are environment variables and why should we care about them? Well, bro, an environment variable is essentially a key-value pair that stores information in your computer’s memory. This information can be used by various programs to customize their behavior or settings based on specific conditions.

Now, how to use environment variables for configuring Python applications. First, you need to set the variable using the `os` module:

# Import the os module to access and manipulate environment variables
import os

# Set a new environment variable called DB_URL with value 'postgresql://user:password@localhost/mydatabase'
# This line uses the os.environ dictionary to set a new environment variable with the key 'DB_URL' and the value 'postgresql://user:password@localhost/mydatabase'
os.environ['DB_URL'] = "postgresql://user:password@localhost/mydatabase"

Once you have set the variable, you can access it in your Python code using `os.environ`. For example:

# Import the os module to access environment variables
import os

# Get the value of the DB_URL environment variable and print it to the console
# Note: The DB_URL variable must be set beforehand for this script to work
print(os.environ['DB_URL'])  # Access the value of the DB_URL variable using the os.environ dictionary and print it to the console

But what if we want to delete an environment variable once it’s no longer needed? Well, you can use the `pop()` method:

# Import the os module to access operating system functionalities
import os

# Set a new environment variable called API_KEY with value 'mysecretkey1234567890'
os.environ['API_KEY'] = "mysecretkey1234567890"

# Use the API key to make an authenticated request
# Function auth_api() takes in the API key as a parameter and makes an authenticated request
auth_api(os.environ['API_KEY'])

# Delete the environment variable once it's no longer needed
# The pop() method removes the specified key and its corresponding value from the environment variables
os.environ.pop('API_KEY')

Now, some best practices for using environment variables in Python:

1. Avoid providing default values as much as possible. Instead, use a configuration file or command line arguments to set the values of your environment variables. This will make it easier to manage and maintain your application over time. 2. Use descriptive names for your environment variables that are easy to understand and remember. For example, instead of using `DB_URL`, you could use `DATABASE_CONNECTION`.

3. Always check if an environment variable exists before accessing it in your code. This will prevent errors and ensure that your application behaves consistently across different environments (e.g., development vs production). 4. Use a tool like Doppler to manage your environment variables for you. Doppler allows you to store all of your secrets and config values in one place, making it easy to share them with team members and collaborators.

SICORPS