CodeNewbie Community 🌱

Cover image for Set, Get, Read and Print Environment Variables in Python
Hichem MG
Hichem MG

Posted on

Set, Get, Read and Print Environment Variables in Python

Environment variables are an essential aspect of modern software development and deployment. They allow you to configure applications and manage settings dynamically, without hard-coding them into your source code.

In this tutorial, we will explore what environment variables are, how to set, get, print, and read them in Python, with practical applications and example.

Let's begin, shall we?

Table of Contents

  1. Understanding Environment Variables
  2. Set Environment Variables
  3. Get Environment Variables
  4. Print Environment Variables
  5. Read Environment Variables from Files
  6. Practical Examples
  7. Security Considerations
  8. Common Pitfalls and How to Avoid Them
  9. Conclusion

1. Understanding Environment Variables

Imagine you're a chef in a bustling kitchen. You have a recipe (your code), but the ingredients (your data and settings) might change. Environment variables are like the labels on the jars in your kitchen. They tell you what's inside without you having to open each one. They are key-value pairs maintained by the operating system, crucial for configuring your applications without hard-coding values.

In the world of software development, environment variables can be used to manage:

  • Application configurations: Database URLs, API keys, debug modes.
  • User-specific settings: User home directories, preferred languages.
  • System settings: Paths to executable files, system directories.

2. Set Environment Variables

Setting environment variables is like putting the right ingredients in your kitchen jars before you start cooking. You can do this directly in your operating system's shell or within your Python script.

In the Shell

For quick setups and testing, you can set environment variables in your terminal.

  • Windows (Command Prompt):
  set MY_VAR=HelloWorld
Enter fullscreen mode Exit fullscreen mode
  • Unix/Linux/Mac (Bash):
  export MY_VAR=HelloWorld
Enter fullscreen mode Exit fullscreen mode

In Python

When you need to set environment variables dynamically or within a script, Python's os module is your best friend.

import os

# Set an environment variable
os.environ['MY_VAR'] = 'HelloWorld'

# Verify that the variable is set
print(os.environ['MY_VAR'])  # Output: HelloWorld
Enter fullscreen mode Exit fullscreen mode

This snippet sets MY_VAR to HelloWorld, just like filling up a jar and labeling it.

3. Get Environment Variables

Retrieving environment variables is akin to checking the labels on your jars before adding ingredients to your dish.

In Python, you use the os module to fetch these values.

import os

# Set an environment variable
os.environ['MY_VAR'] = 'HelloWorld'

# Get an environment variable
my_var = os.environ.get('MY_VAR')
print(my_var)  # Output: HelloWorld

# Get an environment variable with a default value
my_var = os.environ.get('NON_EXISTENT_VAR', 'DefaultValue')
print(my_var)  # Output: DefaultValue
Enter fullscreen mode Exit fullscreen mode

By using os.environ.get, you avoid the risk of your code breaking if the environment variable isn't set, providing a safe fallback with a default value.

4. Print Environment Variables

Sometimes, you need to take stock of what's in your kitchenβ€”checking all the jars and their labels. In the programming world, this means printing all environment variables, which can be very useful for debugging and configuration checks.

import os

# Print all environment variables
for key, value in os.environ.items():
    print(f'{key}: {value}')
Enter fullscreen mode Exit fullscreen mode

Running this script will output every environment variable currently set in your system. This can help you understand the current environment in which your Python script is running, and ensure all necessary variables are correctly configured.

5. Read Environment Variables from Files

For larger projects, managing environment variables directly in the shell or within your scripts can become unwieldy. This is where .env files come into play. They allow you to store all your environment variables in one place, which you can easily update as needed.

Using dotenv Library

The python-dotenv library makes it easy to read variables from a .env file. First, you'll need to install it:

pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

Then, create a .env file in your project directory:

# .env file
MY_VAR=HelloWorld
ANOTHER_VAR=PythonRocks
Enter fullscreen mode Exit fullscreen mode

You can load these variables in your Python script like this:

from dotenv import load_dotenv
import os

# Load environment variables from a .env file
load_dotenv()

# Get environment variables
my_var = os.environ.get('MY_VAR')
another_var = os.environ.get('ANOTHER_VAR')

print(my_var)        # Output: HelloWorld
print(another_var)   # Output: PythonRocks
Enter fullscreen mode Exit fullscreen mode

This approach is particularly beneficial in environments like development and staging, where you might need to frequently change configuration values.

6. Practical Examples

Let's dive into some real-world scenarios where environment variables shine.

Example 1: Configuring a Database Connection

In many applications, you need to connect to a database. Hardcoding credentials in your source code is a bad practice due to security concerns. Instead, you can use environment variables.

import os
from dotenv import load_dotenv
import psycopg2

# Load environment variables from a .env file
load_dotenv()

# Get environment variables
db_host = os.environ.get('DB_HOST')
db_name = os.environ.get('DB_NAME')
db_user = os.environ.get('DB_USER')
db_password = os.environ.get('DB_PASSWORD')

# Connect to the database
connection = psycopg2.connect(
    host=db_host,
    database=db_name,
    user=db_user,
    password=db_password
)

print("Database connection established")
Enter fullscreen mode Exit fullscreen mode

This code connects to a PostgreSQL database using credentials stored in environment variables, making your application more secure and configurable.

Example 2: API Key Management

When interacting with third-party APIs, you often need an API key. Again, using environment variables is the way to go.

import os
from dotenv import load_dotenv
import requests

# Load environment variables from a .env file
load_dotenv()

# Get environment variables
api_key = os.environ.get('API_KEY')

# Use the API key to access a service
response = requests.get('https://api.example.com/data', headers={'Authorization': f'Bearer {api_key}'})
data = response.json()

print(data)
Enter fullscreen mode Exit fullscreen mode

This example demonstrates how to securely manage and use API keys without exposing them in your source code.

7. Security Considerations

Environment variables significantly enhance security, but only if used correctly. Here are some best practices:

  • Do not hard-code secrets: Always use environment variables for sensitive information.
  • Use .env files: Keep configuration details out of your codebase. Ensure your .env file is in .gitignore to prevent it from being pushed to version control.
  • Restrict access: Limit access to environment variables to only those who need it.
  • Encrypt sensitive data: If you're storing environment variables in cloud services, ensure they are encrypted.

8. Common Pitfalls and How to Avoid Them

While environment variables are powerful, there are some common mistakes to watch out for.

Not Handling Missing Variables

If an environment variable isn't set, trying to access it can result in errors. Always provide default values or handle exceptions.

import os

# Safely get an environment variable
db_password = os.environ.get('DB_PASSWORD')
if db_password is None:
    raise ValueError("DB_PASSWORD environment variable is not set")
Enter fullscreen mode Exit fullscreen mode

Inconsistent Environment Across Development and Production

Ensure consistency across different environments (development, staging, production). Use .env files and configuration management tools to maintain this consistency.

9. Conclusion

Environment variables are a simple yet powerful tool for configuring your Python applications. They allow you to manage settings dynamically, enhance security by keeping sensitive information out of your codebase, and ensure your applications are more portable and maintainable.

Whether you are setting, getting, printing, or reading environment variables, Python provides the tools you need to manage them efficiently.

Experiment with different scenarios and see how environment variables can streamline your development and deployment processes. Remember, the right configuration can turn a good application into a great one! Happy coding!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.